Monday, April 30, 2012

NSUserDefaults vs. Core Data for application state

I have an existing large app using plists to store its data. I store application state indicating current item selected, current user, and various other current selections in user defaults. This worked fine when the data was in plists. Now I'm refactoring to use Core Data instead of plists. I want to store a reference to an object instance as the currently viewed object. I know sqlite and Core Data have an ID for this object in the database, but I'm not sure what to store in user defaults. Three options come to mind:




  1. Generate my own sequential ID, store that with the objects I want to remember as "current", store that ID in user defaults.

  2. Try to use NSManagedObjectID and store in user defaults. I "think" I can convert it to a string as follows. Does this work?



    NSString *stringID = [[[managedObject objectID] URIRepresentation] absoluteURL];


  3. I could create a current state entity in Core Data which is a singleton, only one row. This entity could have a to-one relationship to the current object I want to keep track of.




Suggestions appreciated for the approach with best design consideration.





No comments:

Post a Comment