Denis Balko
Denis Balko

Reputation: 1596

Effective use of Core Data and NSManagedObjectContext

I was wondering, and never found any documentation to it, as to, first of all, what is an efficient way of fetching CD objects. In my app I have to store thousands of objects in my NSMOC and need to fetch them as fast as possible. Ois usi g NSPredicate the best way to fetch the entities that fit what I want the best way? Secondly, if I want to keep an "archive" type of thing where I store thousands of objects not used often, but I need to have them, is it possible to have two NSMOCs, and if so, will it speed up the process of fetching entities?

Thanks

Upvotes: 0

Views: 423

Answers (1)

Marcin Kuptel
Marcin Kuptel

Reputation: 2664

If you want to fetch CoreData objects efficiently, you have to consider a few approaches:

  1. If you fetch a lot of data, you should do it on a background thread so that you don't block the UI.
  2. NSFetchRequest has a property called propertiesToFetch which you can use to fetch only the properties you need
  3. You can prefetch some of the relationships for later use using the relationshipKeyPathsForPrefetching property of NSFetchRequest
  4. You should set a predicate on your NSFetchRequest object so that you fetch only the data you need
  5. It is also important to structure your entity graph correctly so that you don't fetch unnecessary data when you don't need it.

You can't say that NSPredicate is the best way to fetch CoreData objects because you use NSFetchRequest for this task, but it is definitely is a good idea to use NSPredicate as well.

I don't think that having a separate MOC used as an "archive" is going to help your performance. You should probably read Apple documentation on CoreData performance

Upvotes: 1

Related Questions