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.