I am using Core Data to store data from the server that is sent in a “relational” form.
For example, the data I get from the server looks something like this (the actual format is somewhat different, but similar enough)
Users: [{PK: 1, Name: 'A B'}, {PK: 2, Name: 'C D'}, {PK: 3, Name: 'E F'}]
Posts: [{PK: 1, UserPK: 1, Content: '...'}, {PK: 2, UserPK: 3, Content: '...'}]
My Core Data model has an relationship set between Users and Posts and it works like it should. However, my problem is creating these objects and their relationships as fast as possible (without using ridiculous amounts of RAM) when I receive a new dataset from the server.
The problem is associating the posts with the users. The “normal” way to do it is to basically write post.user = user; However, this requires that I have the user loaded from the disk.
My current plan is to load all users, fault them and create a NSDictionary that maps the PK’s to the actual objects. That way I can quickly find out the user I need to associate with the post. However, this solution seems a bit complex for something that should essentially be a rather trivial operation.
Your solution of keeping the users in memory in a
NSDictionaryis sound. They should not take very much memory at all as long as you leave them in a faulted state. A faulted MO is tiny.