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

时间:2013-01-20 12:18:50

标签: core-data ios6 nsfetchrequest persistent-storage uimanageddocument

我有一个应用程序,我提出VC1来挑选游戏,VC2提交所选游戏的游戏。当用户从VC2回到VC1时,我想保留他们正在玩的游戏的游戏数据。由于它是iOS 6.0,我使用UIManagedDocument来访问Core Data以存储和检索游戏数据。我完全被我所面临的问题困扰,在花了无数个小时之后,我正在这个论坛上与聪明的人接触。

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

持久性商店的一些日志信息

DEVICE
首先进入VC2
打印文档的说明 - > _persistentStoreCoordinator: 打印文档的说明 - > _persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x1fd28ce0>(

首先在VC2上设置退出VC2
打印文档的说明 - > _persistentStoreCoordinator: 打印文档的说明 - > _persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x1fd28ce0>(  (URL:file:// localhost / var / mobile / Applications / 4DD2D219-5AC1-406F-8020-260B01E46E0C / Documents / GameDB / StoreContent / persistentStore) )

第二次进入VC2
打印文档的说明 - > _persistentStoreCoordinator: 打印文档的说明 - > _persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x211d4660>(

SIMULATOR
首先进入VC2
打印文档的说明 - > _persistentStoreCoordinator: 打印文档的说明 - > _persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x84e4b60>(  (URL:file://localhost/Users/Rujul/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B187169B-8D32-4BB1-AB41-33DB76637D9C/Documents/GameDB/StoreContent/persistentStore) )

首先退出VC2 打印文档的说明 - > _persistentStoreCoordinator: 打印文档的说明 - > _persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x84e4b60>(  (URL:file://localhost/Users/Rujul/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B187169B-8D32-4BB1-AB41-33DB76637D9C/Documents/GameDB/StoreContent/persistentStore) )

第二次进入VC2
打印文档的说明 - > _persistentStoreCoordinator: 打印文档的说明 - > _persistentStoreCoordinator-> persistentStores: < _NSArrayM 0xf777910>(  (URL:file://localhost/Users/Rujul/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B187169B-8D32-4BB1-AB41-33DB76637D9C/Documents/GameDB/StoreContent/persistentStore) )

1 个答案:

答案 0 :(得分:0)

检查代码中的所有覆盖(如UIDocumentSaveForOverwriting中的上述内容)。记录之前和之后的确切值。我相信你会以这种方式找到罪魁祸首。

另外,请检查每个prepareForSegue:中您正在做什么。

顺便说一句,我建议或许将令人困惑的addOrGetDataToGamesDatabase业务重构为两种易于理解的方法。