How to configure RestKit for Core Data persistence?

I'm desperately trying to get RestKit Core Data configuration right but I must be doing something wrong because when I load objects, they are correctly mapped but never persisted. I ended up copy/pasting everything from RKTwitterCoreData example and got to the following code, but I still get nothing in my database:

- (void)configureRestKit {
    //[[AFHTTPRequestOperationLogger sharedLogger] setLevel:AFLoggerLevelDebug];
    //[[AFHTTPRequestOperationLogger sharedLogger] startLogging];

    // Initialize RestKit
    NSString *serverUrl = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFServerUrl"];
    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:serverUrl]];
    objectManager.HTTPClient = [CFHTTPClient sharedClient];

    // Enable Activity Indicator Spinner
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

    // Initialize managed object store
    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    objectManager.managedObjectStore = managedObjectStore;

    // Setup our object mappings
     Mapping by entity. Here we are configuring a mapping by targetting a Core Data entity with a specific
     name. This allows us to map back Twitter user objects directly onto NSManagedObject instances --
     there is no backing model class!
    RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:managedObjectStore];
    eventMapping.identificationAttributes = @[@"eventId"];
    [eventMapping addAttributeMappingsFromArray:@[@"eventId", @"title", @"startDate", @"endDate", @"city", @"country"]];


    // Update date format
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"d/M/yyyy"];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [RKObjectMapping setDefaultDateFormatters:[NSArray arrayWithObject:dateFormatter]];

    // Register our mappings with the provider
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"events" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:responseDescriptor];

    [objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Event class] pathPattern:@"event" method:RKRequestMethodPOST]];
    [objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Event class] pathPattern:@"event/:eventId" method:RKRequestMethodGET]];

     Complete Core Data stack initialization
    [managedObjectStore createPersistentStoreCoordinator];
    NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Chapter4.sqlite"];
    NSError *error;
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSInferMappingModelAutomaticallyOption : @YES, NSMigratePersistentStoresAutomaticallyOption :@YES} error:&error];
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

    // Create the managed object contexts
    [managedObjectStore createManagedObjectContexts];

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


In case you're still looking for an answer, I came across the same problem today in RestKit 0.22.0 and got persistence by manually creating a NSPersistentStoreCoordinator and initializing the RKManagedObjectStore with that instead of a NSManagedObjectContext.

Try this (error checking omitted for clarity):

// Model
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

// Manually create a NSPersistentStoreCoordinator
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];

// Add your persistent store to the coordinator
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Chapter4.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType

// Init the RKManagedObjectStore with the coordinator
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:persistentStoreCoordinator];

// Let it create the NSManagedObjectContexts
[managedObjectStore createManagedObjectContexts];

objectManager.managedObjectStore = managedObjectStore;

Then, assuming you're working in the mainQueueManagedObjectContext, save it with:

[objectManager.managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:nil];

Since RestKit relies on CoreData, you need to save your ManagedObjectContext in order to persist the data. I'm not sure what version you are on, but I saved the data like this:

[[RKObjectManager sharedManager].objectStore.managedObjectContextForCurrentThread save:nil];

Put it inside objectLoaderDidFinishLoading:(RKObjectLoader*)objectLoader, and your objects should be saved.

Need Your Help

Android: executing the thread in a loop with onTouchListener()

android ontouchlistener

Hi in my app there are 8 buttons. Each button is configured onclickListener() when it is clicked the String is written ti the socket. Now i want that when i press and hold the button the String mus...

Is it possible to write tacit functions in F#

f# functional-programming tacit-programming

Tacit or point-free style programming allows one to create functions without regard to their arguments. Can this be done in F#?