What to use to store data objects locally?

Apple stated 'the CloudKit framework should not be used to replace model objects in your app and should not be used for storing objects locally.'

My question is what should be used for storing objects locally?

Obviously I can't use a plain file, it will be slow when records getting more.

SQLite also was reported as slow in the web.

Accepted Reply

OK, for this I would advise to save each image on a file (name made of "someString" + sequentialNumber, use Archiver for stroing data in a single file, including the sequentialNumber to link with the image file.


Never experienced perf issue (even on an old iPhone 5C)

Replies

My data objects are pretty simple, a date field and a few numbers and an image, let's say meals. When I saving them to a file using following, it was slow every time.

NSKeyedArchiver.archiveRootObject(meals, toFile: Meal.ArchiveURL.path)

The image is around 500k per record, but the file saved via archiveRootObject was pretty big, it was almost 15mb per record, don't know why archiveRootObject need so much extra spaces.

How many records do you plan ? hundreds, thousands, more ?


In my case, I have a few hundreds. I store images as individual files (jpg images to limit size) and the rest as an archived file.

I use SQLite for a few things, including storing various BLOBs, and don’t find it too slow. Rather than just believing everything you read on the web, I advise you to test it yourself and then decide. Modern phones have such ridiculously powerful CPUs that unless you have some ***** inefficiencies somewhere (e.g. doing the object decoding repeatedly) I think you’re unlikely to run into performance problems.

the app will generate less than one hundred records each year, it is a small number, but archiveRootObject still slow and space consuming.

OK, for this I would advise to save each image on a file (name made of "someString" + sequentialNumber, use Archiver for stroing data in a single file, including the sequentialNumber to link with the image file.


Never experienced perf issue (even on an old iPhone 5C)

userDefaults is used to store parameters which are re commonly used to determine an app’s default state at startup or the way it acts by default. I don't think it is a fit as the real data object.

no for key-chain data, what is plist?

I can have a try, but I doubt Apple's Cocoa’s archiving infrastructure archiveRootObject can do well with Array type either except UIImage.

I do have array of integers in the data.

Except using NSKeyedArchiver and NSKeyedUnarchiver,

what's the advantage using Core Data Framework?

what's the advantage using Core Data Framework?

Core Data is a really useful technology but it’s not the only way to persist your data. There are lots of other options (user defaults, JSON, property lists, keyed archives, ‘raw’ SQLite, custom file formats, and so on) and the best option very much depends on the nature of your data. This includes things like:

  • How much data you’re dealing with, both in terms of number of items and size of those items?

  • Is there a complex internal structure?

  • How you access that data?

Core Data shines where there’s a lot of data with complex internal structure and you don’t want to load it all into memory at once. From your earlier posts it seems like Core Data isn’t a great fit for your requirements, but rather you’d be better off using a keyed archive for the most of your data and separate on-disk files for your images.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

so use NSKeyedArchiver to save most the data all together and save thumbnail photo and the real photo into separate files?

It will be

#1. one big file for all numeric etc data.

#2. one thumbnail photo each record.

#3. one real photo each record.


When init list, will load all of #1 and #2, will load #3 only going to the detail view.

#1 may be large number of items, but won't be big.

I am also thinking about this because my app generates about 100 records per year.


#1. one file for all numeric etc data each record.

#2. one thumbnail photo each record.

#3. one real photo each record.

To save the data from memory to local disk, should there a [Save] button?

or it should be auto-saved?

If auto-saved, how to save the parent data[usually array of objects] under the deatil view?