开发者

NSManagedObject delete not working, can still retrieve the object

I am trying to d开发者_运维问答elete a managed object, is there something I am missing?

[managedObjectContext deleteObject:managedObject];

NSError *error;

if (![self.managedObjectContext save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    return NO;
}

return YES;

When I run this code the object can still be retrieved.

NSManagedObject *objectiveManagedObject = [managedObjectContext objectWithID:objectID];

return (ObjectiveManagedObject *)objectiveManagedObject; 


What happens if you do

[[self managedObjectContext] setPropagatesDeletesAtEndOfEvent:NO]

first?


I note that you've switched between self.managedObjectContext and managedObjectContext. Are you certain these are the same variable? Avoid accessing your ivars directly; use accessors (except in init and dealloc). This avoids all kinds of problems.

Are you certain that managedObject has the ID objectID? They could be two objects that simply appear similar.


The context was not being saved properly


Given that the previous answers didn't check out, I would say the most likely explanation is that the managed object is the last object in a required relationship. As such, it cannot be deleted until the object on the other end of the relationship is deleted or a second object is added to the relationship.

You might also want to check that the persistent store isn't set to readonly. I don't remember what errors you get when you try to write a readonly store.

Also, you might want to wait a few seconds before testing for the object. Saves are disk operation and therefore relatively slow. It is possible that the save actually fails.

You should be trapping the error return from the save in any case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜