Checking for existing value in NSFetchedResultsController / Core Data
I have some code that calls an API and gets a set of results back, let's call them 'message' objects. Each message has a unique ID.
Unfortunately, I cannot guarantee that the server will not give me back a message I have not previously received and stored in my core data database.
The unique ID is set up as an NSNumber field in my 'Message' entity.
Is there a way 'on-the-fly' to check the database to see if a 'Message' already exists with a given unique ID?
Currently I'm creating an instance of a helper object each time I need to check, and simply creating an NSFetchedResultsController returning filtered results开发者_Python百科 via an NSPredicate. If any results are returned, I move on, or store in the database. I suspect this is a slow approach...
Any help?
Looks like this is what I need:
NSUInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:&error];
This gives me back the count without actually doing the query - much better.
精彩评论