我正在初始化我的模型。如果本地数据库不存在,那么我查询我的后端服务器以检索数据并存储在核心数据中(第一个 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];
}
}