Ok, lets start with the code. I am looping through a returned array of dictionaries and creating (or updating) objects based on them. In this method I’m trying find or create a new entity. And then if the object is supposed to be deleted, I’d like to do so and not waste time updating it with the new information.
- (void)updateOrCreateObjectWith:(NSDictionary*)dictionary {
RKManagedObjectStore *objectStore = ((MyAppDelegate*)[[UIApplication sharedApplication] delegate]).objectStore;
id updateObject = (NSManagedObject*)[objectStore findOrCreateInstanceOfEntity:[resource entity] withPrimaryKeyAttribute:@"myID" andValue:[dictionary objectForKey:@"id"]];
[updateObject setMyID:[dictionary objectForKey:@"id"]];
// if marked for deletion, delete it now
if ([[dictionary objectForKey:@"deleted_at"] isKindOfClass:[NSString class]]) {
if ([updateObject isNew]){
NSError *error = nil;
[objectStore.managedObjectContext save:&error];
if (error) {
NSLog(@"error saving before delete: %@",error);
return;
}
// [objectStore.managedObjectContext deleteObject:updateObject];
// [objectStore.managedObjectCache delete:updateObject];
}
else {
[objectStore.managedObjectContext deleteObject:updateObject];
}
return;
}
[updateObject updateWith:dictionary];
}
The part to be aware of is the deleted_at section with the (1) save section, (2) delete object from context, and (3) delete object from cache. I have tried a few combinations of those three but I don’t get the desired results.
If I delete it from the cache (just #3):
- The objects get saved but they have no attributes.
If I delete it from the managed context (just #2) I get:
NSUnderlyingException=Cannot update object that was never inserted.
Since it was never inserted, I thought I’d save it and then delete it (#1 and #2), but then I get:
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0xed27810 <x-coredata://4EE6AD5A-CC34-460A-A97A-0909454126A4/User/p166>''
So what is the proper way to remove a “new” object from NSMangedObjectContext?
It’s easier to get the
RKManagedObjectStoreinstance with[[RKObjectManager sharedManager] objectStore](assuming you want the shared one, which it seems like you do since you’re calling your app delegate).Check for the
deleted_atkey before you ever create an NSManagedObject. This code assumes you’re converting to typeResource, which is a subclass ofNSManagedObject. It’s untested but should give you an idea of what you should be doing.