移动sqlite文件后,核心数据自动轻量级迁移失败

时间:2013-12-21 13:48:28

标签: ios core-data core-data-migration

在我的应用程序中,我已经将Core Data Automatic Lightweight Migration用于多个版本。大约一年前,我实现了iOS文件共享,所以我将sqlite数据库移动到/ Library文件夹而不是/ Documents目录,以使其隐藏。从那时起我还没有添加另一个Core Data版本。

现在,当我尝试添加另一个实体并增加模型版本时,我收到以下错误(截断以显示尾部):

"_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store}
Terminating app due to uncaught exception     
'NSInternalInconsistencyException', reason: 
'This NSPersistentStoreCoordinator has no persistent stores.  
It cannot perform a save operation.'

我已经验证了.sqlite文件是否按预期存在于磁盘上。我已经进行了几天的故障排除,似乎我将sqlite文件移回/ Documents目录,迁移工作正常。

以下是持久性商店协调员的代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSURL *storeUrl = [NSURL fileURLWithPath: [[self libraryDocumentsDirectory] stringByAppendingPathComponent: @"myappdb.sqlite"]];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self managedObjectModel]];

//Pass options in for lightweight migration....    
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                             configuration:nil URL:storeUrl options:options error:&error]) {
    /*Error for store creation should be handled in here*/
    NSLog(@"ERROR IN MIGRATION........%@", error);        
}

return persistentStoreCoordinator;
}

- (NSString *)libraryDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
}

以前是否有人遇到过这种情况?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

“现在,当我尝试添加其他模型实体并增加版本号时......”

您需要在创建新实体或对现有实体进行更改之前创建新的模型版本。确保您的更改未在旧版本中错误地进行。

答案 1 :(得分:0)

创建版本并将其标记为当前版本后,你必须在用于添加PersistentStore的选项中添加一个额外的值然后你去(我不确定其他iOS版本但是它肯定会在iOS 7上运行)。

-(NSManagedObjectModel *)managedObjectModel
{
    if (managedObjectModel != nil)
    {
        return managedObjectModel;
    }
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    return managedObjectModel;
}

-(NSPersistentStoreCoordinator *)persistentStoreCoordinator
 {

   if (persistentStoreCoordinator != nil)
   {
       return persistentStoreCoordinator;
   }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ABC.sqlite"];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] ini   tWithManagedObjectModel:[self managedObjectModel]];

//Creating Lightweight migration.
    NSDictionary *options =
    @{
      NSMigratePersistentStoresAutomaticallyOption:@YES
      ,NSInferMappingModelAutomaticallyOption:@YES
      ,NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}
     };


   if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
       abort();
    }
return persistentStoreCoordinator;
}