Sebin Roy
Sebin Roy

Reputation: 844

When managed object context get loaded with data from persistent store

I am trying understand core data more.

In words of apple managed object context is a scratchpad. Data will be wrote to file only when managed object save is called. So my doubt is when will managed object context get loaded with data from store. Is it at the time on context initialisation(While adding persistent store coordinator to context) ?

Upvotes: 1

Views: 311

Answers (2)

Sebin Roy
Sebin Roy

Reputation: 844

From apple's managedObjectContext - executeFetchRequest method discussion:-

An object that meets the criteria specified by request (it is an instance of the entity specified by the request, and it matches the request’s predicate if there is one) and that has been inserted into a context but which is not yet saved to a persistent store, is retrieved if the fetch request is executed on that context.'

That means fetch request always happens from persistent store, and if there exist some data in context-1(there can be multiple managed object context present in case of concurrent execution of core data) that is not saved to persistent store(ie. context.save is not called), on fetching data, those data(not saved) will be fetched along with data in persistent store only if execute fetch request is called up on context-1 only. Executing fetch on any context bring those data.

Upvotes: 1

pbasdf
pbasdf

Reputation: 21536

No, the context is not loaded with data from the store when it is initialised. A newly initialised context will have no registered objects.

The data is loaded primarily when you fetch from the store using NSManagedObjectContext's executeFetchRequest method.

But objects can also be loaded automatically if an existing registered object has a relationship to an object that has not yet been fetched.

More rarely, objects can be loaded into the context individually using the objectWithID or existingObjectWithID:error: methods.

Upvotes: 2

Related Questions