Reputation: 343
In Core Data, I have a field called UUID
which is a unique id.
Now, I want to fetch a single specific UUID
record. For that i need to use NSPredicate
. But from what I know, the predicate would search the entire data store. For example suppose there are 10 records and I want to search for the UUID
which has a value of 2
. The predicate will continue the search after it's found 2
(taking extra CPU cycles).
I want to know if we can optimise this. How can I make the predicate stop searching once its found the first object.
Upvotes: 0
Views: 1269
Reputation: 119031
In Core Data, use setFetchLimit:
on the fetch request to limit the number of results. You can also add an index to the UUID attribute if you are going to be using it frequently.
If you want to run the predicate on an array instead and limit the hits you would need to iterate the array yourself (enumerateObjectsWithOptions:usingBlock:
) and explicitly apply the predicate to each item (evaluateWithObject:
). Then, when you find a match, use the BOOL *stop
parameter of the block to end the iteration.
Upvotes: 2