Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

ios - Cannot use a predicate that compares dates in Magical Record

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...