Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data vs NSUserDefaults for logged in user data

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.

like image 297
Georgi Avatar asked Feb 13 '15 14:02

Georgi


People also ask

When should I use Core Data vs UserDefaults?

Core Data only fetches the information it needs to perform the requests the application makes. This is very different from the defaults system. The UserDefaults class loads the property list into memory to improve performance and it asynchronously writes the changes back to disk at appropriate times.

Should I use Core Data or realm?

If your project requires encryption or speed, then Realm is an obvious choice. If your project has a complex data model that changes frequently, then Core Data might be a better choice.

When should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

Is NSUserDefaults secure?

Because NSUserDefaults stores all data in an unencrypted . plist file, a curious person could potentially view this data with minimal effort. That means that you should never store any type of sensitive data inside NSUserDefaults.


2 Answers

I did both things, but for me storing my user in CoreData and saving my user id in the NSUserDefaults 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.

like image 102
amb Avatar answered Sep 30 '22 12:09

amb


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.

like image 35
narner Avatar answered Sep 30 '22 10:09

narner