0

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

Klemen
  • 2,144
  • 2
  • 23
  • 31

3 Answers3

1

I think you want a framework that has a different philosophy from that reflected in the way Apple designed Core Data. The framework is meant to be an object graph (as opposed to a "database"). The context serves as a "scratch pad" to make changes, independently from any storage details.

It makes sense that view controllers have access to the objects they display, read their properties and change the in response to user interaction. The context is a convenient construct to manage multi-threading, the undo-manager and other great features.

The paradigm you describe is more along the lines of ActiveRecord. This is really a matter of preference because there are advantages and drawbacks to both approaches. (I do not want to expand on those because this would be mainly opinion based, not suitable for the StackOverflow format.)

If you want something like user.save(), take a look at MagicalRecord. This is a very popular framework that tries to bring the ActiveRecord idiom to Core Data. I am not a fan, though I have used it a couple of times without major problems.

Mundi
  • 79,884
  • 17
  • 117
  • 140
1

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.

Community
  • 1
  • 1
Bretsko
  • 608
  • 2
  • 7
  • 20
0

If you want to save just the Current User object, I think the easiest approach is using NSUserDefaults.

You can easily save the object and retrieved it, just some lines of code are required.

Check out this tutorial, it may help you to implement it.
http://swiftcoder.me/2014/08/17/persisting-data-with-nsuserdefaults/

Ulysses
  • 2,281
  • 1
  • 16
  • 24
  • Ok, for Current User it's easier to save it your way. But I need my approach for some other objects in greater quantity. I just explain it on Current User scenario for the sake of simplicity. – Klemen Jan 31 '16 at 19:42