iOS:编码/解码内存密集型对象的最佳方法
我对 iOS 编程相当陌生,正在努力确定使用 NSCoding 协议对内存密集型对象进行编码的最佳方法。
我有大量的 Item 对象。每个项目都有许多与之相关的高分辨率图像。此外,每个 Item 都属于一个 ItemCategory,该 ItemCategory 可能包含 100 个 Item。
据我所知,我有几个不同的编码选项:
- 对整个 ItemCategory 对象进行编码
- 消除 ItemCategory 类,为每个 Item 创建一个 itemCategory 属性,然后对各个 Item 对象进行编码。
在我看来,#1 会非常昂贵。为了将新项目添加到 ItemCategory,我必须解码整个 ItemCategory(这意味着也解码与其包含的项目相关的数百个图像),添加项目,然后重新编码整个项目(再次,连同所有这些图像)。
但是,从代码结构的角度来看,#1 似乎确实是正确的方法。 #2 迫使我想出一种不太直观的方法来存储项目并将它们与各自的项目类别相关联。
如果我选择#1,有没有办法只解码对象的某些部分,这样当我实际上不需要显示它们时,我就不会最终初始化所有这些图像?我想到的一个想法是,实际上并不将 Item 的 UIImages 与 Item 本身一起编码,而只是对图像名称进行编码。这样,图像只会在必要时进行初始化,并且如果需要,可以在不释放整个 Item 的情况下释放图像。我想这是一种关系数据库类型的方法。
我觉得必须有一个标准的方法来处理这样的情况,不是吗?
或者我对内存消耗的恐惧是没有根据的?也许这可以被视为“过早优化”的一个例子,但我现在做出的决定将深刻影响应用程序的数据结构。从选项 #1 更改为选项 #2 的效果可不太好:)
I'm fairly new to iOS programming and am struggling to decide on what is the best way to encode memory intensive objects using the NSCoding protocol.
I have a large number of Item objects. Each Item has numerous hi-res images associated with it. Additionally, each Item belongs to an ItemCategory, which may contain 100 Items.
As far as I can tell, I have a couple different encoding options:
- Encode the entire ItemCategory object
- Eliminate the ItemCategory class, create just an itemCategory property for each Item, and just encode the individual Item objects.
It seems to me that #1 would be wastefully expensive. In order to add a new Item to the ItemCategory, I'd have to decode the entire ItemCategory (which means decoding those hundreds of images tied to the Items it contains as well), add the Item, and then re-encode the whole thing (again, along with all those images).
But, #1 does seem to be the correct way to do it from a code-structure point of view. #2 forces me to come up with a less intuitive way for storing Items and associating them with their respective ItemCategories.
If I were to go with #1, is there a way to decode only certain parts of objects, so that I don't end up with all those images getting initialized when I don't actually need to display them? One thought that occurred to me is to not actually encode the Item's UIImages along with the Item itself, but rather just the image name. That way, the image would only get initialized when necessary, and could be released without releasing the entire Item if so desired. I suppose this is kind of a relational database type of approach.
I feel like there must be a standard way for handling a situation like this, no?
Or is my fear over memory consumption unfounded? Perhaps this could be seen as an example of "premature optimization", but the decision I make now will deeply affect the data structure of the application. Changing from option #1 to #2 down the road wouldnt be pretty :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以使用 CoreData 和 persistenceStore。 CoreData 使管理关系变得容易。您可以创建具有由系统管理的关系的实体。因此,在您的情况下,也许您可以有 3 个实体:类别、项目和图像,以及类别和项目之间的一对多关系,以及项目和图像之间的一对多关系,这意味着每个类别可以有多个项目,并且每个项目都有多个图像,那么当您需要时,您可以创建新图像并将它们添加到特定项目,或搜索项目中的所有图像。
我希望这一点很清楚,但 CoreData 非常适合管理关系,并且数据模型非常易于使用。
Perhaps you can use CoreData, and a persistentStore. CoreData makes it easy to manage relationships. You can create entities that have relationships that are managed by the system. So in your case, perhaps you can have an 3 entities: Category, Item, and Images, and a one to many relationship between Category and items, and a one to many relationship between Item and images, meaning that each category can have multiple items, and each item has multiple images, then when you need yo you can create new images and add them to a particular item, or search for all the images within an item.
I hope that this was clear, but CoreData is great for managing relationships and the data model is very easy to work with.