问题
I am making a method that will remove all of my NSManagedObjects that were not updated in the last sync.
- (void)removeStaleObjects {
// Delete objects that have not been updated in this sync.
NSPredicate *stalePredicate = [NSPredicate predicateWithFormat:@"updated < %@", self.syncStart];
NSArray *staleObjects = [Node MR_findAllWithPredicate:stalePredicate];
for (Node *n in staleObjects) {
[[NSManagedObjectContext MR_defaultContext] deleteObject:n];
}
}
The code keeps failing on the MR_findAll... line with
[__NSDate objCType]: unrecognized selector sent to instance
I have checked my syntax with the apple documentation and I am 99% positive that I am creating the predicate correctly, startDate is just
_startDate = [NSDate date];
that gets run prior to my sync. then after my sync I call
[self removeStaleObjects];
Does anyone know where I am messing up?
Update: I was able to get it to work by storing the update time as a double. However, I am still interested in getting it to work with NSDates so if anyone figures something out, please post it here.
回答1:
The problem is the name "updated" of your attribute. It conflicts with the isUpdated
method of NSManagedObject
. (See Core Data NSPredicate "deleted == NO" does not work as expected for a similar issue with a "deleted" attribute.)
If you rename your attribute, everything works as expected.
回答2:
It also looks to my that your predicate is formatted correctly. Here are a couple things you can do:
1) When debugging this, print out that predicate. You should see something like:
updatedDate < {some integer value}
Dates are stored as integers under the covers, and a predicate converts it properly as well. If your predicate isn't printable in the debugger, you'll know right away
2) Check your updatedDate type. Make sure that's a date (I trust it's already a date, but you didn't specify in your question)
3) Make sure your Node object has the updatedDate attribute on it.
来源:https://stackoverflow.com/questions/16591982/cannot-use-a-predicate-that-compares-dates-in-magical-record