简体   繁体   中英

for the given configuration should I attempt to use NSUserDefaults or go to CoreData?

For the given configuration I need to be able to store should I consider using NSUserDefaults, or go to CoreData?

The configuration for the iPhone would be roughly like this, note the 3 levels so to speak:

  • Items ( NSMutableArray )
    • aString: NSString
    • aString2: NSString
    • aDate: NSDate
    • aDate2: NSDate
    • aBool: BOOL
    • aTI1: NSTimeInterval
    • aTI2: NSTimeInterval
    • Keywords ( NSMutableArray )
      • keyword: NSString
      • keyword: NSString

PS New to iPhone so haven't got any invested time into any particular persistance approach yet.

Looking at the structure, i would recommend you to use NSDictionary to save the data at specific keys.

whether to use Core Data or NSUserDefault depends whether you want the data to be saved on a plist and be available every time you start your application or to share the data among the classes without saving it in a file, respectively.

NSUserDefaults and Core Data perform two different task and you should pick the one appropriate to the data you wish to persist.

Despite the historical name, the NSUserDefaults are intended as the application preferences. ( In the old Next days, there was complicated set of network based preference domains of which the NSUserDefaults was merely the bottom/local most set of preference. In iOS, they have become just the preferences and the enclosing domains have been removed or hidden. ) If the data you wish to persist regards the application state eg users choice of UI layout (such as fonts or startup mode), last open view, last viewed web page, then that information should go into the defaults.

Core Data by contrast is designed to implement the entire data model layer of a Model-View-Controller design app (which all iOS apps are.) It's primary task is not persistence/saving but modeling/simulating the logic and relationships connected to the app's data. Really, the model layer is the guts of the apps logic and the controllers and views just interface.

As such, Core Data is overkill for saving little pieces of data like you have. You can certainly do it but it's like driving a semi-truck down to the QuickyMart to haul back a quart of milk.

Perhaps more importantly, if the data is application state data then it needs to be in the user defaults so that (1) the data is available when the app starts and (2) it is available anywhere in the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM