How do I set up Core Data lightweight migration using MagicalRecord?

余生长醉 提交于 2019-12-04 23:08:16

问题


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:

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?


回答1:


The whole point of MagicalRecord is that this is managed for you:

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:####];

Check the docs about the Core Data stack setup here.




回答2:


Make sure you check all of these things:

In your AppDelegate.m file:

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

If you haven't versioned your model already:

  • Select your data model

  • Editor -> Add Model Version

  • Name the new version, Finish

  • There should be two versions now. Select the file as shown.

  • Change the Model Version to your new version

  • The new version should be checked now




回答3:


As of my understanding of the question, i suggest you to use this

[MagicalRecord setupAutoMigratingCoreDataStack]

If you have not changed the model version, change it to new model created from the old model



来源:https://stackoverflow.com/questions/16841158/how-do-i-set-up-core-data-lightweight-migration-using-magicalrecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!