6

I have hit a brick wall trying to setup lightweight migration of Core Data using MagicalRecord. I have looked at all of the posts on this subject, using Google and SO. I understand how the persistentStoreCoordinator works and what the settings I am trying to make also do.

Here's my code:

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;

}

I'm getting the following errors, which I understand; what I don't know is where are these objects (I have looked in my app, and found nothing):

No visible @interface for 'AppDelegate' declares the selector 'applicationDocumentsDirectory' and

No visible @interface for 'AppDelegate' declares the selector 'managedObjectModel'

I have already created the stores:

xcdatamodeld

Most, if not all of the code I have looked at is similar; I don't know if MagicalRecord handles this for me or not because I can't find any docs that would indicate how to do this using MR. My question is: what do I have to do to make this work?

4

3 回答 3

13

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

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:####];

在此处查看有关核心数据堆栈设置的文档。

于 2013-05-30T16:39:30.537 回答
8

确保检查所有这些内容:

在您的 AppDelegate.m 文件中:

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

如果您尚未对模型进行版本控制:

  • 选择您的数据模型

选择您的数据模型

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

    在此处输入图像描述

  • 命名新版本,完成

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

  • 现在应该检查新版本

在此处输入图像描述

于 2015-08-27T20:55:23.360 回答
6

根据我对这个问题的理解,我建议你使用这个

[MagicalRecord setupAutoMigratingCoreDataStack]

如果您没有更改模型版本,请将其更改为从旧模型创建的新模型

于 2014-09-25T05:34:14.417 回答