Firstly, the base code that a Core-Data sets you up with can make the whole thing very confusing. The thing to understand is that Core-Data is a kind of wrapper around a variety of database technologies (sqlite, binary, xml) and by doing this, relieves you of the need to directly touch the database. The main class you are going to worry about in the beginning is NSManagedObjectContext
. Think of it as a snapshot of the underling database which you can modify as you please and when you are done write that NSManagedObjectContext
onto the database. The other classes you have there are really only needed for more fine-grained, low level control, and since this is your first app, best to leave those alone. You should read this, it is large, but you will gain a lot of understanding from it, especially how everything connects and their role.
To summarise though:
- Really you only need to pass around the
NSManagedObjectContext
- You can do this my making an instance variable in every View Controller, OR,
- You shouldn't go making more than one per app, unless you really need to, and then you need to make sure you merge them back together
- You don't really need to create a repository of all the objects because
NSManagedObjectContext
does this for you...kinda... it will load objects into memory conditionally, there is a lot to this but the place to start is learning what a fault is (all in Apples documentation underNSManagedObjectContext
orNSManagedObject
) - think about what your objects represent and do. You can subclass
NSManagedObject
to represent your Core-Data object and place logic and validation inside of it - super handy. - Look into classes like
NSFetchRequest
andNSPredicate
which are the two core classes for getting objects out of theNSManagedObjectContext
. - Look into classes like
NSFetchedResultsController
which can tie very nicely into UI objects likeUITableView
.
Finally, Core-Data is a beast, and often you find yourself repeating common tasks all the time. You should look into this excellent framework which adds all sorts of helpers (like an easily accessible instance of NSManagedObjectContext
, and one line object fetching, creating, deleting).