Search code examples
iosobjective-ccore-datansmanagedobjectcontext

How to implement NSManagedObjectContext inside application model instead of view controller?


Im wondering if I can make my application structure in a way to store User object in Core Data, without knowing to much about NSManagedObjectContext from the view controller perspective.

I want to do it like they do it in some of the frameworks like Parse for instance. Where you can save its objects locally with simple methods like [object save] or [object pin].

I know that for some more complex objects I'll definitely want to have a control over NSManagedObjectContext inside view controller. But in case ob just saving User as currentUser and retrieve later, I really want to somehow incapsulate NSManagedObjectContext inside the object itself.

So instead of a method
[User createCurrentUserInContext:self.context] or
[User currentUserInContext:self.context]

I just want to call
[User createCurrentUser] or
[User currentUser]

Can you give me some advice with this. I'm looking forward to hear as many solutions as possible from you.

Thank you for your help


Solution

  • If you need a big local store of objects, you'll have to deal with the database, SQlite3, and CoreData is a nice wrapper which simplifies operations with SQlite3. Parse Local Datastore is implemented with SQlite3, so object pinning is basically calling to save data through NSManagedObjectContext in SQlite db.

    If you need a higher level of abstraction over CoreData you can use a wrapper for CoreData, check here for some popular libraries. Any sensitive data, like user credentials, should be stored in a keychain.