uchicago-mobi / 2016-Winter-Forum

Class Forum for MPCS51030.
3 stars 0 forks source link

Persistent data storage #224

Closed ghost closed 8 years ago

ghost commented 8 years ago

Tried archiving my data singleton to NSURL, received SNAFU.

Is it safe or harmful to store the app's data in NSUserDefaults if all the data is relevant to the app's basic behaviour? I recognize that CoreData or a structured DB is better if the app is generating big files which are only occasionally accessed, but that is not the case.

tabinks commented 8 years ago

You have to make sure that all of the data types you are trying to save to nsuserdefaults are compatible. Types such as strings and arrays are compatible. Y ou can save your own custom objects to default but you need to use keyed archiving which is a protocol of Nscoding.

Take a look at the project I put in the playgrounds repository, it shows an example of saving a custom object using keyed archiving.

ghost commented 8 years ago

Excellent. One other question: I have implemented NSCoding for a custom classes but encapsulated instances cannot be cast to NSData. Do all member instances of a class to be encoded as NSData also need to inherit from NSObject, NSCoding?

tabinks commented 8 years ago

That is exactly correct. Every object you want to encode to nscoding must conform to nscoding.

ghost commented 8 years ago

Thanks! My app now has persistent data and can pass an NSData object to another device.