NSInternalInconsistencyException此NSPersistentStoreCoordinator没有持久性存储

时间:2013-01-12 06:07:04

标签: iphone core-data ios6 nspersistentdocument

我正在初始化我的模型。如果本地数据库不存在,那么我查询我的后端服务器以检索数据并存储在核心数据中(第一个if语句)。但是我得到" NSInternalInconsistencyException当我尝试保存时,NSPersistentStoreCoordinator没有持久存储" 例外(即[self.gameDB saveToURL:self.gameDB.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL]

- (void)setUpGameModel
{

    if (!self.gameDB) {
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"GameDatabase"];
        self.gameDB = [[UIManagedDocument alloc] initWithFileURL:url];
    }

    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.gameDB.fileURL path]]) {
       // does not exist on disk, so create it

        dispatch_queue_t fetchQ = dispatch_queue_create("New Game Fetcher", NULL);

        dispatch_async(fetchQ, ^{
            [self.gameDB.managedObjectContext performBlock:^{ // perform in the NSMOC's safe thread (main thread)
                [Game queryGameFromServerWithLevel:@"1" inManagedObjectContext:self.gameDB.managedObjectContext];
                [self.gameDB saveToURL:self.gameDB.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
            }];
        });

    } else if (self.gameDB.documentState == UIDocumentStateClosed) {
    // exists on disk, but we need to open it
        [self.gameDB openWithCompletionHandler:^(BOOL success) {
           [Game queryGameWithLevel:@"1" inManagedObjectContext:self.gameDB.managedObjectContext];
        }];
    } else if (self.gameDB.documentState == UIDocumentStateNormal) {
        // already open and ready to use
        [Game queryGameWithLevel:@"1" inManagedObjectContext:self.gameDB.managedObjectContext];
    }

}

0 个答案:

没有答案