简体   繁体   中英

User Defaults - Objective C

I'm currently working on a project that will rely on saved data to be used properly.

It will consist of a saved array (id) starting at 0 and incrementing in values for each saved type, then working in parallel to this will be another array that will store the data required for each id.

What I'm asking is, before I make a mess of my project.. As it's my first attempt at doing so, would I be better off taking an approach in the following;

MySQL to store the data types and id (Can I also do the php connection, retrieval, input, delete etc.. using PDO::)?

A single userDefaults array with sub-keys? ( If it's possible )

Two arrays working in parallel with the keys matching for data-types and id's?

NSMutableArray *masterArray;
NSArray *loadMaster = [userDefaults arrayForKey:[NSString stringWithFormat: @"%@",userSelectedService]];
if (loadMaster != nil) {
    masterArray = [loadMaster mutableCopy];
} else {
    masterArray = [[NSMutableArray alloc] initWithCapacity:100];
}

Or am I going down the wrong path completely for what I need to achieve?

You have lots of options for object graph persistence; it depends on how much data you are storing, the complexity of your object relationships, how you intend to access them.

  1. Your objects could adopt the NSCoding protocol and you could archive them to a data file stored in the appropriate place in your app's file system.
  2. You could use Core Data . There's a particularly good set of articles on Core Data at objc.io
  3. You could use SQLite, with or without an Objective-C abstraction layer such as FMDB .

And others. Depending on the amount of data you wish to store, perhaps NSUserDefaults is an acceptable solution. But if you are interested in storing lots of data, then having to read the entire object graph into memory is not ideal.

Having two arrays that happen to be in sync sounds like a bad idea.

You are not very specific about the nature of the data to be saved, but for simple key:value pairs, just use an NSDictionary . If the data model is (even slightly) more complex, you will enjoy using CoreData. The learning curve is a little steep, but it's worth your time.

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