ios迁移核心数据
我已经阅读了文档并遵循了中的步骤- 但在运行代码时仍会出现如下错误ios迁移核心数据,ios,objective-c,core-data,coordinate,Ios,Objective C,Core Data,Coordinate,我已经阅读了文档并遵循了中的步骤- 但在运行代码时仍会出现如下错误 -(void)loadPhotoGallery:(NSURL *)path { if (self.defaultPhotoLibrary != nil) return; // this should handle auto migrations - according to documentation NSURL * url; if (path == nil) { u
-(void)loadPhotoGallery:(NSURL *)path
{
if (self.defaultPhotoLibrary != nil) return;
// this should handle auto migrations - according to documentation
NSURL * url;
if (path == nil)
{
url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"defaultPhotoDatabase"];
} else {
url = path;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path1];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error]) {
// Printing this error
NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}
UIManagedDocument *lib = [[UIManagedDocument alloc] initWithFileURL:url];
if (![[NSFileManager defaultManager] fileExistsAtPath:[lib.fileURL path]]) {
[lib saveToURL:lib.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success)
{
// Great
} else {
//bad
}
}
else if (lib.documentState == UIDocumentStateClosed) {
[lib openWithCompletionHandler:^(BOOL success) {
if (success) // here is the failure when migrating - because the DB file exists
{
//great!
} else {
//bad - getting into this code
}
}
我得到的错误是:
PersistentStoreCoordinator出现问题:错误域=NSCOCAERRORDOMAIN Code=256“操作无法完成。(Cocoa错误256)。”用户信息=0xe09a880{NSUnderlyingException=无法打开数据库文件,NSSQLiteErrorDomain=14}
但是数据库文件确实存在(在“elseif”中),因此不清楚问题出在哪里?要帮助您排除故障,请打开Core Data Migration Debug模式,如以下帖子的答案所示: