0

Based on some limited testing, I see that if I

  1. Execute a Fetch request with result type = NSDictionaryResultType
  2. Do some manipulations on the returned values
  3. Store back the MOC on which Fetch request was executed

the changes in step 2 are not written back to the persistent store because I am changing a dictionary and not a "managed object". Is that a correct understanding?

Smart Home
  • 801
  • 7
  • 26

2 Answers2

1

Most likely you are abusing the dictionary result type. Unlike in conventional database programming, you are not wasting valuable memory resources when fetching the entire objects rather than just one selected attributes, due to an under-the-hood mechanism called "faulting".

Try fetching with managed object result type (default) and you can very easily manipulate your objects and save them back to Core Data. You would not need to do an additional fetch just to get the object you want to change.

Consider dictionaries only in special situations with huge data volumes, difficult relational grouping logic, etc., which make it absolutely necessary.

(That being said, it is unlikely that it is ever absolutely necessary. I have yet to encounter a case where the necessity of dictionaries for fetches was not an indirect result of flawed data model design.)

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks, Is it true that it is faster to just retrieve ManagedObjects and then do the filtering on the in-memory ManagedObjects (rather that using a Pedicate during the Core-Data Fetch). If yes, how do I set a predicate on managed object? That is one reason I usually use Result type as Dictionary since I know how to do subsequent filtering. – Smart Home Dec 04 '15 at 06:49
  • Quite the opposite. You should of course fetch as few records as possible by using predicates in the fetch request. Very often, in-memory filtering is then reduced to a minimum. As for speed, I think that my method might be faster than the dictionaries, but this needs to be validated by tests. – Mundi Dec 04 '15 at 08:46
0

Yes, kind of, you can't store a dictionary back into the context directly so you can't save any updates that way.

If you get a dictionary object then you need to include in it the associated managed object id (if it isn't aggregated) or do another fetch to get the object(s) to update.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks. Could you please illustrate the first approach "include in it the associated managed object id". Based on your answer, it looks with this I can get the fetch results back as dictionary, make changes and store it back to data store without additional fetch requests. Is that correct? – Smart Home Nov 30 '15 at 17:35
  • See this answer http://stackoverflow.com/a/4792331/1988185 and yes, you just use get existing object on the context. – Wain Nov 30 '15 at 17:43