Reputation: 674
I have an app where I already use Core Data to store a big amount of shops, which a user can view once logged in. I am currently implementing the login part of the app and I looked through many related questions. Most of them suggest using NSUserDefaults
for storing non sensitive user data. I am planning to store things like first name, last name, email, profile picture and maybe address. The list may grow in future. My question is it a valid option to create a User entity
in my Core Data Model in order to store a single user object or it will be a bad practice and I should just make it with NSUserDefaults
? I am tempted to use Core Data since I already have everything set up and it would not add extra complexity to the implementation.
Upvotes: 5
Views: 2317
Reputation: 3221
I was in a very similar situation that you were in while developing a recent app. I found that NSUserDefaults
was a more appropriate solution for storing user information, since that's what it's explicitly meant to be used for. As the Apple documentation on it says, 'The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences.'
From my experience, using NSUserDefaults to store user info rather than Core Data meant having a lot less code. Additionally, you don't have the overhead of having to access the Core Data store every time your user logs into your app).
For reference, I used this and this when I was learning where to use NSUserDefaults.
Upvotes: 3
Reputation: 3274
I'll go with NSUserDefault if you plan to have only one user at the time. In one of our apps we need to have the ability to manage multiple users accounts running at the same time. We builded a different db for each user using the UserId as name identifier. Letting us remember the state of the app for each user So I think the answer will depend on the scenario. Just remember to not save valuable information on the userDefault. Maybe things like email, username, etc would be better if are saved on the app key ring. There are some pods that make the saving and retrieving from the keyring as easy as access to a dictionary.
Good luck.
Upvotes: 0
Reputation: 4856
I did both things, but for me storing my user in CoreData and saving my user id in the NSUserDefault
s was the better solution. I did this because I was using both Mantle and Overcoat to parse the server response and my user had exactly the same structure as any other user in the app.
Upvotes: 4