iOS RestKit can not save local entity to database
Asked Answered
T

1

12

I am using RestKit 0.20 to parse JSON data and save to database. THere is a mapped entity SchoolClass, which is handled by RestKit and saves fine. I have another entity called MyClass, which stores the classes I have selected. This one is only local on the device.

Here is the code I create and save the MyClass entity

 NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
 MyClass* course = [managedObjCtx insertNewObjectForEntityForName:@"MyClass"];

 .. set the data for course here

 NSError *executeError = nil;
 if(![managedObjCtx save:&executeError]) {
      NSLog(@"Failed to save to data store");
 }

Here is the code that initialize the managed data store

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

   /**
     Complete Core Data stack initialization
     */
    [managedObjectStore createPersistentStoreCoordinator];
    NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKMainDb.sqlite"];
    NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
    NSError *error;
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil 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];

It appears the save is successful and in the MyClasseTableViewController I could read the saved MyClass entries. However after I close the app and restart again. The MyClassTableViewController is empty, because the fetched results is empty. I opened the sqlite file using SQLiteBrowser and the MyClass table is empty. Looks like the MyClass entities are only saved in the cache but not in the persistent store. Do I need to call some API provided by RestKit to save it? I tried to read through the doc but could not find it. Please help.

Thi answered 24/3, 2013 at 0:29 Comment(2)
Are you using nested managed object contexts? If so, you'll need to save the root context to get changes saved to the data store.Burford
hi Tom, thanks for the input. I added some code to the question that shows how the store is created. I use the RKManagedObjectStore class to do that, and always get the managedObjectContext from it. Perhaps RestKit is using something nested. I will dig deeper follow your lead. Thanks.Thi
T
24

Thanks for the lead by Tom, I found that RestKit has NSManagedObjectContext (RKAdditions), which has a method:

- (BOOL)saveToPersistentStore:(NSError **)error

Yes it does have logic to handle nested managed object context. Here is the new code that works, just one line change, but took a lot of time to find the right call :(

#import "NSManagedObjectContext+RKAdditions.h"
     NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
     MyClass* course = [managedObjCtx insertNewObjectForEntityForName:@"MyClass"];

     .. set the data for course here

     NSError *executeError = nil;
     if(![managedObjCtx saveToPersistentStore:&executeError]) {
          NSLog(@"Failed to save to data store");
     }
Thi answered 25/3, 2013 at 20:8 Comment(2)
THANK YOU SO MUCH!!!! :( so much time spent searching everywhere for this!! Thank you thank you!!!Cashbook
THANK YOU. been using the framework for a long time now but still forgotten this. lolProtection

© 2022 - 2024 — McMap. All rights reserved.