NSKeyedArchiver encodes only part of an array

I have a list of objects that can sometimes change, and I want to keep a constant cache on the device whenever the application closes or moves to the background. Most of the objects in the list will not change, so I was wondering what is the best way to save the list. I have two main options that I think of:

  • Using NSKeyedArchiver / unArchiver is the most convenient method, because the objects that I serialize store other custom objects, so I can just write my own encoding method for each of them. The main problem is that I did not find on Google how to serialize only modified objects, and serializing the entire list every time seems very wasteful.

  • Using SQLite is what I am using now, and the worst problem is that adding / changing properties of objects is very complex and much less elegant.

Is there a way I can take advantage of the convenience of NSKeyedArchiver, but only serialize modified objects?

+4
source share
1 answer

Like Adam Ko, I would suggest using Core Data:
In the end, this problem is what it is really good at!

If your cache elements are independent of each other, this can be achieved simply by transferring your cache elements with a thin layer of NSManagedObject (i.e. you can benefit from Core Data with minor changes to your application).

This wrapper object can store an archived version of a cache element in an attribute of type NSBinaryDataAttributeType and provide access to an unarchived object through a transient property. See Custom Permanent Attributes for an example.

0
source

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


All Articles