Core Data NSPredicate "deleted == NO" does not work as expected
Solution 1:
The problem is that you have defined an attribute deleted
for your entity. That conflicts with the isDeleted
method of NSManagedObject
, so you should rename that attribute.
The following "experiment" shows that strange things happen if you call your attribute "deleted" (c
is a managed object with a custom deleted
attribute):
// Set custom "deleted" property to YES:
c.deleted = @YES;
// Use the property, as your Code 1
NSLog(@"%@", [c deleted]);
// Output: 1
// Use Key-Value Coding, as your Code 2
NSLog(@"%@", [c valueForKey:@"deleted"]);
// Output: 0
// Now really delete the object and try again:
[context deleteObject:c];
NSLog(@"%@", [c valueForKey:@"deleted"]);
// Output: 1
Your "Code 1" refers to the property, therefore it returns the expected result. "Code 2" uses Key-Value Coding, and [c valueForKey:@"deleted"]
returns YES
if the object
actually has been deleted from the context!
So renaming that attribute should solve your problem. Unfortunately the compiler does not emit warnings if an attribute name conflicts with a built-in method.