after I insert a ManagedObject into a context I'd like to fetch it later but before saving the context (I'd save after all objects are inserted). It appears that querying the context later with a fetch concerning those objects returns nothing if the context wasn't previously saved. Is there a way to save only in the end ?(I guess i can save my objects in an array or dictionary and query that but i thought coredata would do this for me)
Asked
Active
Viewed 3,361 times
7
-
This can also fail if you are using the NSDictionaryResultType http://stackoverflow.com/questions/1632029/nsdictionaryresulttype-expression-not-taking-into-account-newly-inserted-objects – Daniel Galasko May 07 '15 at 09:18
1 Answers
19
Try this:
[myFetchRequest setIncludesPendingChanges:YES];
From the documentation:
Sets if, when the fetch is executed, it matches against currently unsaved changes in the managed object context.

Mike Weller
- 45,401
- 15
- 131
- 151
-
I saw that before but it seems not to work, the fetch returns a 0 element array but the context shows 20 objects in it (I log it with [[moc insertedObjects] count]), while if i saved before the moc contains only 1 inserted object (I inserted it before fetching, but nevermind) and the array of fetched objects has length of 1 (which is correct). Maybe it is because my context is bounded to a persistent store, shall I link it to an in-memory store? – rano Jul 13 '10 at 09:25
-
This should work regardless of where the store is. If you have multiple context, remember that you can fetch only the unsaved objects inside each individual context until each context has saved to the store. – TechZen Jul 13 '10 at 14:29
-
3includesPendingChanges defaults to YES according to the documentation so this doesn't really help... – Daniel Galasko May 07 '15 at 09:15