I am using RestKit for my rest implementation. I fetch the data from the server and upon receiving it, I call saveToPersistentStore
which is successful as well but when I turn off internet and load that page again, it doesnt find any data in the store.
Here is the initialization flow -
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Storage" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
// Initialize the Core Data stack
[managedObjectStore createPersistentStoreCoordinator];
NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Failed to add persistent store: %@", error);
[managedObjectStore createManagedObjectContexts];
// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:managedObjectStore];
I use the mainQueue for everythng -
controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
This is the function which does the fetch:
[CMLRKSharedManager getNewsForUser:userProfile
urlParams:dict
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self.tableView reloadData];
NSLog(@"Fetched object count %lu %@ %@", (unsigned long)self.fetchedResultsController.fetchedObjects.count , self.fetchedResultsController, self.fetchedResultsController.managedObjectContext);
NSManagedObjectContext *managedObjCtx = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSError *executeError = nil;
if(![managedObjCtx saveToPersistentStore:&executeError]) {
NSLog(@"Failed to save to data store");
}
else
NSLog(@"SAVE SUCCESSFUL");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[self.tableView reloadData];
}];
}
Output:
2014-04-02 22:24:01.547 Fetched object count 4
2014-04-02 22:24:01.555 SAVE SUCCESSFUL
Now I kill the app, turn off wifi and launch app again. The table is now blank and I see that the fetchResultsController
is seeing 0 items in console log
Apr 2 22:24:36 : In numberOfRows Fetched object count 0
I'm not sure why...
@wain I update the code as follows -
NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Cover" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
// Initialize the Core Data stack
[managedObjectStore createPersistentStoreCoordinator];
Commented these 2 lines
// NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
// NSAssert(persistentStore, @"Failed to add persistent store: %@", error);
//
Instead added the sqlite db but now the app crashed
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}