Reputation: 1108
I'm using RestKit 0.20 to fetch JSON Requests into Core Data. For some reason I have to delete all objects in an entity (i.e. myEntity). I'm doing so with this code:
NSManagedObjectContext *moc = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
[moc saveToPersistentStore:nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"myEntity"
inManagedObjectContext:moc];
fetchRequest.includesPropertyValues = NO;
NSArray *entries = [moc executeFetchRequest:fetchRequest error:nil];
for (myEntity *entity in entries) {
[moc deleteObject:entity];
}
[moc saveToPersistentStore:nil];
After that, the next RKObjectRequestOperation output is "(200 OK / 0 objects)". But in fact, there are about 700 objects in the JSON document.
If I call [[NSURLCache sharedURLCache] removeAllCachedResponses];
at the end, I get all 700 objects, but I have 2 RestKit Errors (133000): E restkit.core_data.cache:RKEntityByAttributeCache.m:227 Failed to retrieve managed object...
Can anyone help me how to do this this the right way?
EDIT: This is how I create the core data stack:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myDataModelName" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[self.managedObjectStore createPersistentStoreCoordinator];
[self.managedObjectStore
addSQLitePersistentStoreAtPath:self.pathToDatabase
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:@{ NSInferMappingModelAutomaticallyOption: @YES,
NSMigratePersistentStoresAutomaticallyOption: @YES }
error:nil];
[self.managedObjectStore createManagedObjectContexts];
NSManagedObjectContext *moc = self.managedObjectStore.persistentStoreManagedObjectContext;
self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:moc];
Is there a difference between managedObjectStore.persistentStoreManagedObjectContext
and managedObjectStore.mainQueueManagedObjectContext
?
Upvotes: 3
Views: 1346
Reputation: 119031
Preferably you should specify a fetch request block on the object manager and allow RestKit to handle the deletion of the objects. The fetch request block allows RestKit to find all of the objects that need to be deleted during the mapping process while maintaining any internal caching and maximising data reuse. Check the docs here (particularly section "Fetch Request Blocks and Deleting Orphaned Objects").
Yes, there is a difference between managedObjectStore.persistentStoreManagedObjectContext
and managedObjectStore.mainQueueManagedObjectContext
.
The header Cache-Control: public, max-age=180
that the server returns tells the URL caching system not to request new results for any request that was made less than 3 minutes ago. So, during that time, RestKit will not actually send a request and will get objects from the model instead.
Ideally you should change the returned headers. But, effectively, that should really be done automatically because your request should be different if it's for a different user to the one who was previously logged in.
Upvotes: 1