Undistraction
Undistraction

Reputation: 43402

Lightweight Migration with RestKit

Apple's docs give the following example for setting up an (automatic) lightweight migration:

NSError *error = nil;
NSURL *storeURL = <#The URL of a persistent store#>;
NSPersistentStoreCoordinator *psc = <#The coordinator#>;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

BOOL success = [psc addPersistentStoreWithType:<#Store type#>
                    configuration:<#Configuration or nil#> URL:storeURL
                    options:options error:&error];
if (!success) {
    // Handle the error.
}

However I am using RestKit, which handles the creation of the persistant store behind the scenes. A simplified version of my initialisation code looks like this:

// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:rootURL];

// Create the object store
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName                 
                                                                 usingSeedDatabaseName:seedDatabaseName 
                                                                    managedObjectModel:nil //Don't need to pass it in. It is infered  
                                                                          delegate:self];
// Create Mappings
...

// Define Relationships
...

// Set Mappings
...

Where should I pass in configuration options given that RestKit creates the persistantStore behind the scenes?

Upvotes: 3

Views: 1577

Answers (2)

Khawar
Khawar

Reputation: 9241

You can add these options in RKManagedObjectStore.m, in createPersistentStoreCoordinator method.

For verion 0.10 of RestKit, it is already added, not sure for latest version. But if not already added you can add youself. The final look of the method would be like this.

- (void)createPersistentStoreCoordinator
{
    NSAssert(_managedObjectModel, @"Cannot create persistent store coordinator without a managed object model");
    NSAssert(!_persistentStoreCoordinator, @"Cannot create persistent store coordinator: one already exists.");
    NSURL *storeURL = [NSURL fileURLWithPath:self.pathToStoreFile];

    NSError *error;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_managedObjectModel];

    // Allow inferred migration from the original version of the application.
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(managedObjectStore:didFailToCreatePersistentStoreCoordinatorWithError:)]) {
            [self.delegate managedObjectStore:self didFailToCreatePersistentStoreCoordinatorWithError:error];
        } else {
            NSAssert(NO, @"Managed object store failed to create persistent store coordinator: %@", error);
        }
    }
}

I have tested this in my project, by adding 3 new entities and renaming an old entity, and is working perfect without deleting previous app from device. Hope this helps you.

Upvotes: 2

flashfabrixx
flashfabrixx

Reputation: 1183

In my understanding RestKit is sitting on top of Core Data. So even when you're using a seeded database and let RestKit assign the object store for the object manager, the sqlite database that's provided by Core Data will be used.

To enable lightweight migration with RestKit, you can use the - (NSPersistentStoreCoordinator *)persistentStoreCoordinator method in the AppDelegate (see this thread)

AppDelegate

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil) return __persistentStoreCoordinator;

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"thenameofyoursqlite.sqlite"];

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

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

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
        NSLog(@"Auto migration failed, error %@, %@", error, error.userInfo);
        abort();
    }
}

Upvotes: 2

Related Questions