0

我有一个应用程序,我在其中展示 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
Printing description of document->_persistentStoreCoordinator: Printing description of document->_persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x1fd28ce0>(

)

首先在 VC2 中设置退出 VC2
Printing description of document->_persistentStoreCoordinator: Printing description of document->_persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x1fd28ce0>( (URL: file://localhost/var/mobile/Applications/4DD2D219-5AC1- 406F-8020-260B01E46E0C/Documents/GameDB/StoreContent/persistentStore) )

二进VC2
Printing description of document->_persistentStoreCoordinator: Printing description of document->_persistentStoreCoordinator-> persistentStores: < _NSArrayM 0x211d4660>(

)

SIMULATOR
首先进入VC2
Printing description of document->_persistentStoreCoordinator: Printing description of document->_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
Printing description of document->_persistentStoreCoordinator: Printing description of document->_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))

4

1 回答 1

0

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

另外,检查您在每个prepareForSegue:.

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

于 2013-01-20T20:30:00.273 回答