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
?