2013-05-30 33 views
5

我碰到一堵砖墙,试图使用MagicalRecord设置Core Data的轻量级迁移。我已经使用Google和SO查看了关于此主题的所有帖子。我了解persistentStoreCoordinator是如何工作的以及我想要做的设置。如何使用MagicalRecord设置Core Data轻量级迁移?

这里是我的代码:

AppDeligate.h

NSPersistentStoreCoordinator *persistentStoreCoordinator; 

AppDelegate.m

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

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

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"saori.sqlite"]]; 

// handle db upgrade 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

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

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 

    // Handle error 
} 

return persistentStoreCoordinator; 

}

我收到以下错误,WHI我明白了;我不知道是在哪里这些对象(我已经看过我的应用程序,并没有发现什么):

为“AppDelegate的”不可见@interface声明选择“applicationDocumentsDirectory”和

无为 'AppDelegate的' 可见@interface声明选择 'managedObjectModel'

我已经创建了专卖店:

xcdatamodeld

大部分(如果不是全部)我看过的代码都是相似的;我不知道MagicalRecord是否为我处理这个问题,因为我找不到任何可以指示如何使用MR的文档。我的问题是:我需要做些什么来完成这项工作?

+0

在上一张截图中,新模型尚未成为活动模型,因此仍会使用旧模型。 –

回答

11

MagicalRecord的全部意义在于,这是为你管理:

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:####]; 

检查对核心数据堆栈设置here的文档。

+0

是的,我读过,但没有意识到MR会为我做任何事情!我已经有了你的代码了;谢谢... 还有一个问题:如果我不使用DEBUG标志,而是自己创建新模型,MR仍然会找到它,或者正在使用DEBUG标志来通知MR它需要做的事情移民?如果是这样的话,当应用程序在AppStore中时,我该如何处理? – SpokaneDude

+0

通过将某些标志传递给核心数据来处理迁移。这会导致核心数据检查可用的型号版本,并使用它们自动检测并修复差异。 – Wain

+0

MagicalRecord会加载你的包中的所有模型,并将它们合并在一起,除非同一模型有多个版本,然后它只会将最新版本与其他版本合并。这是+ [NSManagedObjectModel managedObjectModelsFromBundle:]方法中的默认核心数据行为,这是MagicalRecord也用于加载模型的内容。 – casademora

6

由于我的问题的了解,我建议你使用这个

[MagicalRecord setupAutoMigratingCoreDataStack]

如果你没有改变的模型版本,将其更改为从旧的模式创造了新的模式

8

确保你检查了所有这些东西:

在你的AppDelegate中。M档:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    ... 
    [MagicalRecord setupAutoMigratingCoreDataStack]; 
    ... 
} 

如果您还没有版本控制模型已经:

  • 选择你的数据模型

Select your data model

  • 编辑 - >添加模型版本

    enter image description here

  • 名称的新版本,完成

enter image description here

  • 应该有两个版本了。如图所示选择文件。

enter image description here

  • 改变模型版本到新版本

enter image description here

  • 新版本现在应该检查

enter image description here