Frage

I'm integrating Restkit .20 into an existing iOS app and have everything setup (seemingly) correctly and working with my initial model. Creating new objects, storing them, retrieving them, etc., are all working. Before I deploy this new version of the app using restkit/code data, I wanted to test a simple lightweight migration to ensure future changes will work, but when I do I always get the "Can't find model for source store" error when creating my persistent store. Here is my reskit setup code:

- (void)setupRestKit {
    // set up the managed object and set the serialization type to json
    _objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [self getServerBase]]]];
    [_objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];

    // create our managed object models and store, and set the store on the manager
    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    _objectManager.managedObjectStore = managedObjectStore;

    <...all of my entity mapping and response descriptor code is here...>

    // tell our store to create a persistent store coordinator
    [managedObjectStore createPersistentStoreCoordinator];

    // create the path to the database
    NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"<path>.sqlite"];

    NSError *error;

    // these options allow for lightweight migration
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption: @(YES),
                              NSInferMappingModelAutomaticallyOption: @(YES)
                            };

    // create our sqlite persistent store with this path and options
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:options error:&error];

    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

    // tell our store to create the object contexts
    [managedObjectStore createManagedObjectContexts];

    // Configure a managed object cache to ensure we do not create duplicate objects
    managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

To create a new version of my data model, I selected the current model and went to "Editor" -> "Add Model Version". After duplicating the original model, I added a string property "test" to one of my objects, generated new objects from my model by selecting the new data model and hitting command-n and selecting NSManagedObject subclass. I then selected the new model and all of the objects and created them.

After creating the new model, changing one of the object properties, and re-creating all the objects based on this model, I changed the model version in the File Inspector to match the new model I created, and ran the project - where I got the above error. Anyone know/see what I'm doing wrong?

War es hilfreich?

Lösung

Tom was correct - My issue was:

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

As I said, my project was a pretty big one, so I didn't realize that there were existing data models in it already, so the managed object model that I was creating when setting up Restkit was attempting to merge all of them together in the bundle and that clearly wouldn't work. My solution was to change the code such that I was only creating the managed object model with the specific model I was using for Restkit as follows:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"];
NSURL *url = [NSURL fileURLWithPath:path];

NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];

Now, if I want to make changes to the model, I click on the newest version of the model I'm currently using, go to Editor -> Add Model Version and base the new model on the prior model. After making changes to the objects in the model, I make the newest model the "current" model under Model Version in the file inspector, and when I run the project it works great!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top