Storing objects in iOS for later use

My application retrieves JSON data from our web service. In all cases, up to now, the information would be stored in memory or simply updated on the fly, without the need to save anything locally, except for the login token used in the API. Using one of the new functions that we add, we will take in the group of locations, 26 max, with a long, lat, radius and name.

I also need to add 1-2 fields to this data to create a larger object. So my question is: what is the best way to store this type of data in the iOS file system? I am currently using NSUserDefaults, but this seems limited or poorly informed about more data. Probably no.

This data will need to be found, modified or edited and saved. All this while maintaining the ability to pull out any of these 26 objects. Thank you in advance for reading and help.

+6
source share
3 answers

For such a small amount of data (26 points) I suggest archiving .

Save to plist using NSKeyedArchiver / NSKeyedUnarchiver . Read the data from the didFinishLaunchingWithOptions delegate and listen to the UIApplicationWillResignActiveNotification to save it.

A NSUserDefaults is a plist with functions designed to store user settings. It is often used instead of the usual plist to save a couple of lines of code, which I think is a bad idea, because you get additional complexity that is not related to your task.

If you want the login to be protected from the fact that someone stole the device, and Keychain is used to conduct a forensic examination. You can use the wrapper and read some articles, comment if you are interested.

If you are looking for additional features, see The best way to store data on iphone , but it doesn't seem to be that way.

Some code to get you started ... Register to call save on app resign:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveMyData) name:UIApplicationWillResignActiveNotification object:nil]; 

On each graphic / dictionary / object, no matter what you want to archive, implement NSCoding:

 - (void)encodeWithCoder:(NSCoder*)coder { [coder encodeObject:myIvar forKey:kmyIvar]; } - (id)initWithCoder:(NSCoder*)coder { if((self = [super initWithCoder:coder])) { self.myIvar = [[coder decodeObjectForKey:kmyIvar] retain]; } return self; } 
+5
source

Check out this master data guide. This is the best way to store data locally on your device. This is the native cocoa API, and it is compatible with bindings. In addition, you can choose whether to store data as XML, SQLite and Binary.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

For something distant big, I would use this.

+3
source

I had the same question, and I understood a much better solution.

What you can do is simply save the JSON string as NSUserDefault. Then, when you reload the application, use the same method (or framework utility) that you used to map the JSON string to your objects for the first time. That way you can still take advantage of NSUserDefaults.

If you use RestKit to manage your web services, this is even easier. The answer to this post shows how to use the RestKit JSON parser to map to JSON on your object.

Deamination of local NSString JSON to objects via RestKit (no network boot)

+1
source

Source: https://habr.com/ru/post/894482/


All Articles