Deciding to use Core Data or NSUserDefaults

2019-01-24 03:51发布

问题:

I have a feature on my app that allows users to invite their friends (via facebook, or friends in their address book). Most people will have < 5K friends with some people having more (maybe a max of like 10K friends?).

I want to keep track of the friends they have invited so they do not re-invite them. To accomplish this I am saving a dict of friends in NSUserDefaults to store this information. I am wondering if NSUserDefaults will suffice for this, or if I need to use Core Data.

Also, I am planning on adding a feature to allow them to invite friends to a particular event. (there are many events on our app.) If I want to keep track of which friends have been invited to which event, should I be using Core Data then? Will NSUserDefaults suffice for that? (I am assuming it won't). And lastly, should Core Data be used for that or should that be saved server side?

回答1:

NSUserDefaults is really meant for storing small pieces of data such as settings, preferences, and individual values.

You should use Core Data to store a large list of elements. As far your last question, there is nothing preventing you from using both Core Data and a backend to store your data. In fact, there are frameworks out there to facilitate exactly this. Take a look at RestKit.



回答2:

Your assumption is correct! NSUserDefaults is not sufficient and reliable to store and query the huge amount of data. It's suggestable if you'll have a backend (database on the server) to store events and their invitees to persist consistency of user's information (if user logged in back to your app from other app supportive device then he'll get all information he stored). Also create the tables in database in a way that you can query for your required information easily. Something like, for each of the events, there's some field which stores/update its invites information. When you want to know your single query should come with all your need result. That'll put really good effect of your app on the user that your app is running quite fast.



回答3:

What do you plan to save in your dictionary? If it's only s string then it's ok, but if you plan to store images, I would recommend Core Data.