如何使用MagicalRecord设置Core Data轻量级迁移?

我尝试使用MagicalRecord来设置Core Data的轻量级迁移。 我已经使用Google和SO查看了关于这个主题的所有post。 我了解persistentStoreCoordinator是如何工作的,以及我想要做的设置。

这是我的代码:

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; 

}

我收到以下错误,我明白了; 我不知道的是这些对象在哪里(我看了我的应用程序,什么都没find):

'AppDelegate'没有可见的@interface声明select器'applicationDocumentsDirectory'和

'AppDelegate'没有可见的@interface声明select器'managedObjectModel'

我已经创build了商店:

xcdatamodeld

如果不是我所看到的大部分代码都是相似的, 我不知道MagicalRecord是否为我处理这个问题,因为我找不到任何可以指示如何使用MR的文档。 我的问题是:我需要做些什么才能做到这一点?

MagicalRecord的全部要点是,这是为你pipe理的:

 [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:####]; 

在这里查看关于核心数据堆栈设置的文档。

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

在您的AppDelegate.m文件中:

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

如果您还没有版本化您的模型:

  • select你的数据模型

选择你的数据模型

  • 编辑器 – >添加模型版本

    在这里输入图像说明

  • 命名新版本,完成

在这里输入图像说明

  • 现在应该有两个版本。 如图所示select文件。

在这里输入图像说明

  • 将模型版本更改为新版本

在这里输入图像说明

  • 新版本应该现在检查

在这里输入图像说明

至于我对这个问题的理解,我build议你使用这个

[MagicalRecord setupAutoMigratingCoreDataStack]

如果尚未更改模型版本,请将其更改为使用旧模型创build的新模型