Core data 获取核心数据在模拟器中工作,但不在设备上工作

Core data 获取核心数据在模拟器中工作,但不在设备上工作,core-data,ios6,nsfetchrequest,persistent-storage,uimanageddocument,Core Data,Ios6,Nsfetchrequest,Persistent Storage,Uimanageddocument,我有一个应用程序,其中我展示VC1来选择游戏,VC2来提交所选游戏的游戏。当用户从VC2切换回VC1时,我希望保留他们正在玩的游戏的游戏数据。因为它是iOS 6.0,所以我使用UIManagedDocument访问核心数据以存储和检索游戏数据。我完全被我所面临的问题难住了,在花了无数个小时之后,我在这个论坛上向智者们伸出了援助之手 当我在模拟器中运行下面的代码时,一切正常,数据被存储,如果用户选择与前面相同的游戏,我也能够检索并显示它。不幸的是,在设备上,我可以看到数据存储在segue上-我放置

我有一个应用程序,其中我展示VC1来选择游戏,VC2来提交所选游戏的游戏。当用户从VC2切换回VC1时,我希望保留他们正在玩的游戏的游戏数据。因为它是iOS 6.0,所以我使用UIManagedDocument访问核心数据以存储和检索游戏数据。我完全被我所面临的问题难住了,在花了无数个小时之后,我在这个论坛上向智者们伸出了援助之手

当我在模拟器中运行下面的代码时,一切正常,数据被存储,如果用户选择与前面相同的游戏,我也能够检索并显示它。不幸的是,在设备上,我可以看到数据存储在segue上-我放置了一个断点,并使用iExplorer查看了persistentStore-但当我回到VC2选择存储的游戏时,persistentStore似乎被覆盖或清除了所有数据。在调试器中,我注意到在设备上执行检索时,“UIManagedDocument”的_persistentStoreCoordinator对象的_PersistentStoresNSArray属性始终显示0

非常感谢您的帮助

- (void) addOrGetDataToGamesDatabase:(CNPUIManagedDocument *)document withFlag:(BOOL)getFlag {    
    if (getFlag) {
    NSLog(@"INSIDE addDataToGamesDatabase to get data");
    }
    else
    NSLog(@"INSIDE addDataToGamesDatabase to set data");

    dispatch_queue_t update = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    dispatch_sync(update, ^{
    [document.managedObjectContext performBlockAndWait:^{
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Game"];
        request.predicate = [NSPredicate predicateWithFormat:@"game = %@", self.selectedGameID];
        NSError *error = nil;
        NSLog(@"Persistent Store Name = %@", [[document class] persistentStoreName]);
        NSArray *matches = [document.managedObjectContext executeFetchRequest:request error:&error];       
        NSLog(@"Fetch Error if any: %@ %@", error.debugDescription, [error userInfo]);


        if (!matches || matches.count >1) {
        NSLog(@"There is a problem with creating the game data");
        }

        //This is where it fails on the device as the match.count is always 0 as the fetch retrieves nothing
        else if ([matches count] == 0) {
        if (!getFlag) {
        // Code to initialize the game data in store
        }
        }
        else if ([matches count] == 1) {
        // Another fetch - nested
        if (!oTeammatches || [oTeammatches count] >1) {
            NSLog(@"There is a problem with creating the offense team data");
        }
        else if ([oTeammatches count] == 0) {
            if (!getFlag) {
              //Code to initialize the team data in store
           }
        }
        else if ([oTeammatches count] == 1) {
            OTeam *newOTeam = [oTeammatches lastObject];
            if (getFlag) {
                //Retrieves data
                //Shown by log lines beginning with "Getting"
            }
            else {
                //Sets/Saves data
                //Shown by log lines beginning with "Setting"
            }
         }

        }
    }];
    if (!getFlag) {
        [document updateChangeCount:UIDocumentChangeDone];
    }
    });
}

- (IBAction)pressBackButton:(UIButton *)sender {
    [self addOrGetDataToGamesDatabase:self.gamesDatabase withFlag:NO];
    if ((self.gamesDatabase.documentState & UIDocumentStateEditingDisabled) != UIDocumentStateEditingDisabled) {
    [self.gamesDatabase saveToURL:self.gamesDatabase.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
        if (success) {
           NSLog(@"DB save file path: %@", self.gamesDatabase.fileURL);
           NSLog(@"Saved!!!");
        }
        else
        NSLog(@"Unable to save");
    }];
    }
  [self performSegueWithIdentifier:@"BackToPickGame" sender:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  //Some Initialization code

    if (!self.gamesDatabase) {
      NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
      url = [url URLByAppendingPathComponent:@"GameDB"];
      self.gamesDatabase = [[CNPUIManagedDocument alloc] initWithFileURL:url];
      NSLog(@"DB File URL generated: %@", url);
      NSLog(@"self.gameDatabase initialized");
    }

   [self addOrGetDataToGamesDatabase:self.gamesDatabase withFlag:YES];
   NSLog(@"Calling game with ID %@ , self.selectedGameID);
}
有关持久存储的一些日志信息

设备
首先进入VC2
打印文档说明->\u persistentStoreCoordinator: 打印文档说明->\u persistentStoreCoordinator->persistentStores: (

)

退出VC2时在VC2中首次设置
打印文档说明->\u persistentStoreCoordinator: 打印文档说明->\u persistentStoreCoordinator->persistentStores: ( (网址:file://localhost/var/mobile/Applications/4DD2D219-5AC1-406F-8020-260B01E46E0C/Documents/GameDB/StoreContent/persistentStore) )

第二次进入VC2
打印文档说明->\u persistentStoreCoordinator: 打印文档说明->\u persistentStoreCoordinator->persistentStores: (

)

模拟器
首先进入VC2
打印文档说明->\u persistentStoreCoordinator: 打印文档说明->\u persistentStoreCoordinator->persistentStores: ( (网址:file://localhost/Users/Rujul/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B187169B-8D32-4BB1-AB41-33DB76637D9C/Documents/GameDB/StoreContent/persistentStore) )

退出VC2时第一次设置
打印文档说明->\u persistentStoreCoordinator: 打印文档说明->\u persistentStoreCoordinator->persistentStores: ( (网址:file://localhost/Users/Rujul/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B187169B-8D32-4BB1-AB41-33DB76637D9C/Documents/GameDB/StoreContent/persistentStore) )

第二次进入VC2
打印文档说明->\u persistentStoreCoordinator: 打印文档说明->\u persistentStoreCoordinator->persistentStores: ( (网址:file://localhost/Users/Rujul/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B187169B-8D32-4BB1-AB41-33DB76637D9C/Documents/GameDB/StoreContent/persistentStore)
)

检查代码中的所有覆盖内容(如上面的
UIDocumentSaveforOverwrite
中所述)。记录前后的精确值。我相信你会用这种方法找到罪犯的

另外,检查您在每个
prepareforsgue:
中正在做什么


顺便说一句,我建议将混乱的
addOrGetDataToGamesDatabase
业务重构为两种易于理解的方法。

Thx供您参考。不确定覆盖后要我检查哪些值。当我在退出VC2时设置值时,我检查设备上的persistentStore,我看到它被填充。我已经添加了一些关于persistentStore状态的信息。如果您能查看并发表评论,将不胜感激。