1

I'm developing an app that can have multiple users. I mean, when a user downloads the app, he'll have 1 user ( default ), but he can add more users for his parents/sons/wife/etc. And each user will have his own data.

So my question is: What is the best way to handle these users' data ? I was thinking about using Core Data but i 'think' it's really complicated to add a new attribute to the Users entity ( i'm not sure! ). So, now i'm thinking about using NSUserDefaults ( this might be less complicated.. ).

I hope you can help me with this, even if i have to use/do something else.

Thank you, Bader Al-Rasheed

Bader Al-Rasheed
  • 458
  • 1
  • 7
  • 13
  • What type of data will you be storing? In my mind this makes a difference because if your storing a small number of things (user settings, maybe a couple of objects) then it may make sense to serialize an object to a file rather than core data. – Brandon Jun 28 '14 at 01:51

2 Answers2

3

Do not use NSUserDefaults for data. Maybe one string here and there, but NEVER an entire data model.

As for building this with Core Data, it is fairly straightforward. You need to create a User entity with all necessary attributes and then you can create, insert, update, and delete your records using standard Core Data.

I made an example application that uses CoreData. Feel free to check it out: https://github.com/Jakenberg/CoreDataExample

jakenberg
  • 2,125
  • 20
  • 38
  • 2
    This answer could be better if it offered an explanation for why using `NSUserDefaults` would be bad rather than this ultimatum. And better still if it provided some of the actual sample code in the answer itself for that inevitable day when the link dies. – nhgrif Jun 28 '14 at 01:37
  • ^Agree. `NSUserDefaults` is not meant to be doing heavy lifting. You should use it to save some data when app is going to be interrupted or background. Like saving high score/current score of an app etc. – rohan-patel Jun 28 '14 at 02:07
0

Is there any overlap between users' data? If not, then use a separate datastore for each user. You could use NSUserDefaults to keep track of who the current user is, and then point your NSPersistentStoreCoordinator to the appropriate file when the user changes. Use a different URL for each user in the call to NSPersistentStore's initWithPersistentStoreCoordinator:configurationName:URL:options.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42