iosdevnyc
iosdevnyc

Reputation: 1871

Best way to Check if records exists in Core Data DB

I am working on an iOS app and I want to loop through data to check to see if the records exists in the Core Data DB.

I have about 100 records that I want to loop through and see if the record exists.

Should I use NSPredicate to make a fetch request on every loop, or should I make the fetch request once for all the items in the DB and put them in an Array and do the check on an array? I guess my real question is what is expensive on the device to do?

thank you

Upvotes: 0

Views: 1341

Answers (1)

Rog
Rog

Reputation: 17170

You should make one request to fetch all your records and then loop through the returned array using fast iteration.

I guess my real question is what is expensive on the device to do?

The answer to this question is actually to let CoreData do the management for you. It will load the objects as "faults" which do not take up much memory and only load them from the DB when you access them.

With 100 records, you will not hit limits either way (performance or memory) assuming your objects are of a reasonable size. If you objects are very large (say images) CoreData might not be the best way to store them.

Upvotes: 1

Related Questions