开发者

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

开发者 https://www.devze.com 2023-02-19 20:33 出处:网络
For the given configuration I need to be able to store should I consider using 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 roughl开发者_如何学JAVAy 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 useCore 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 e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消