开发者

NSKeyedArchiver encode only part of an array

开发者 https://www.devze.com 2023-01-31 12:49 出处:网络
I have a list of objects that can sometimes change, and I want to keep a persistent cache on the device whenever the app is closed or move to th开发者_开发问答e background.

I have a list of objects that can sometimes change, and I want to keep a persistent cache on the device whenever the app is closed or move to th开发者_开发问答e 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 major options i think about:

  1. Using NSKeyedArchiver / unArchiver - This is the most convenient method, because the objects i'm serializing hold other custom objects, so this way i can just write a custom encode method for each of them. The major problem is that i didn't find on Google how to serialize only the changed objects, and serializing the entire list every time seems very wasteful.

  2. Using SQLite - this is what i'm currently using, and the worst problem here is that adding \ changing properties of the objects is very complicated, and much less elegant.

Is there any way that i can enjoy the convenience of NSKeyedArchiver but only serialize the changed objects?


Like Adam Ko I would suggest using Core Data:
This kind of problem is what it's really good at, after all!

If your cache items are independent from each other, this could be achieved by simply wrapping your cache-items by a thin layer of NSManagedObject (i.e. you could benefit from Core Data with only minor changes to your app).

This wrapper entity could store an archived version of a cache item in an attribute of type NSBinaryDataAttributeType and provide access to the unarchived object through a transient property. See Non-Standard Persistent Attributes for an example.

0

精彩评论

暂无评论...
验证码 换一张
取 消