核心数据中的索引关系 [英] Indexed Relationships in Core Data

查看:81
本文介绍了核心数据中的索引关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在iPhone SDK上使用Core Data,我正在寻找保存有序列表,像数组。但是,Core Data中的关系在检索时表示为集合。这使得难以保存对象被定位的顺序。

I'm just starting out with using Core Data on the iPhone SDK and I'm looking into saving an ordered list, something like an array. However, relationships in Core Data are expressed as Sets when retrieved. This makes it difficult to save the order in which the objects are positioned.

如果允许重新排序项目,则表视图中的数据项就是一个很好的例子。一个简单的解决方案是在被管理对象上包含一个索引属性。

A good example would be data items in table view when re-ordering of items are allowed. An easy solution would be to include an index property on the managed object.

考虑下面的层次结构:

文档< -Many-to-many-> DataItem

Document <-Many-to-many-> DataItem

不同的Document实例可以链接到同一个DataItem,每个Document可能引用一个或多个DataItem。因此,在DataItem中具有索引属性将导致该实例的可重用性较低,即,您只能保存Document的一个实例的索引。

Different Document instances could link to the same DataItem, and each Document might reference one or more DataItem(s). Hence, having an index property in DataItem would lead to less reusability of that instance, i.e. you can only save the index for one instance of Document.

任何想法如何可以呈现层次结构在表视图中整齐排序,但仍保持每个DataItem实例可重用?谢谢!

Any ideas of how I can present the hierarchy neatly ordered in a table view but still keep each DataItem instance reusable? Thanks!

推荐答案

一个好的解决方案是在Document中保留一个单独的数据结构,将DataItems映射到表视图中的一个位置。除了允许相同的DataItem存在多个位置,如果你需要添加一个DataItem到多个文档,这个解决方案也将工作。

A good solution is to keep a separate data structure in Document to map DataItems to a position in the table view. Besides allowing the same DataItem to exist in multiple positions, if you need to add a DataItem to multiple Documents this solution will also work.

回到我在看不同的策略保持核心数据对象的顺序我发现了一个博客文章,解释如何做到这一点非常详细,包括示例代码。

Back when I was looking at different strategies of keeping Core Data objects ordered I found a blog post that explains how to do this in great detail, including sample code too.

这篇关于核心数据中的索引关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆