Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does application state belong in Core Data instead of NSUserDefaults?

I'm trying to decide whether some application state, specifically the selected item in a list, should be stored in Core Data or NSUserDefaults.

Generally I believe that application preferences and state should persist in NSUserDefaults, and model-level data should persist elsewhere, say in Core Data. My model so far is:

  • Should the data be stored at all? If the user wouldn't reasonably expect it to be, then throw it out (for example, the cursor position is not saved in TextEdit)
  • NSUserDefaults:
    1. If the application were multi-document, the setting would apply to all documents
    2. It's conceivable that the data would be configured in preferences
    3. Having the data outside of the model makes sense for testing (swapping several defaults with one model store)
  • Model-level
    1. The data clearly belongs as an attribute of a model-level object
    2. The data is sufficiently large that storing it in NSUserDefaults would cause performance problems
    3. It would be difficult or time-intensive for the user to re-create the data (they would definitely consider the loss of this information "data loss")

I plan to store the sort order of some entities in Core Data. Without this information (i.e. a "sortIndex" or "order" attribute) each entity instance would have to be augmented with data from the user defaults.

However, storing state in the model seems like a slippery slope. If I store sort order then it also seems appropriate to store selection since they are both the state of a list. The selection data for my use case may actually be quite large. Specifically, the icons in one list depend on the selection in each of their sub-lists.

Does anyone have a hard line they draw with respect to NSUserDefaults vs. data model?

like image 844
orque Avatar asked Aug 13 '09 05:08

orque


People also ask

When to Use Core Data vs User defaults?

Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

When to Use Core Data in Swift?

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.


1 Answers

You didn't mention whether this is a document-based app (like say, TextEdit) or a library-based one (like say, AddressBook).

That may help you decide where such information should go: assume a document-based app. Assume its documents get placed under version-control (this is actually feasible when using Core Data's XML data store type). Open the app, change the doc's sort orders. Does this dirty the document? Would this change be worth a check-in? Would the change be valuable to other users of this repository?

Typically, sort orderings aren't valuable enough to warrant document-based storage (ala NSTableView's Auto Save Name in Interface Builder). But your app may place a priority on sorting (it sounds like it).

So, there is no hard-and-fast rule. But I think the idea of having a document under version control, potentially shared with others, provides a good intellectual framework to make your case for either side.

like image 199
rentzsch Avatar answered Nov 04 '22 18:11

rentzsch