有序CoreData记录 [英] Ordered CoreData records

查看:103
本文介绍了有序CoreData记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我知道CoreData不保留记录的顺序,因为他们被添加。因此,如果我们要在CoreData中存储对象数组,我们应该像orderId一样添加字段到对象并手动填充。但是它看起来底层的sqlite基础存储索引的记录,所以可能有一种方式来存储有序对象,而不添加额外的字段?

As I know CoreData does not preserve order of records as they were added. So if we want to store array of objects in CoreData we should add field like orderId to object and fill it manually. But it looks that underlaying sqlite base stores index for records, so may be there is a way to store ordered objects without adding extra field?

推荐答案

简短回答:NO。
长答案:

Short answer: NO. Long answer:


  • 为了保存订单,必须存储订单信息。它可以是隐式的,使用现有属性上的类别描述符,如ShiShi所建议的。

  • in order to preserver order, order information must be stored. It can be implicit, using a sort descriptor on an existing property as suggested by ShiShi. Or you can add a new property for the explicit purpose of maintaining a sort order on the instances.

SQLite会维护一个隐含的主键rowId,如果有一个不是由客户端代码定义的。但是如果客户端代码定义了一个整数主键,SQLite将使用它作为它的rowId。换句话说,在空间或时间上没有优势,试图更聪明,并使用SQLite的rowId。请注意,您可以告诉Core Data您的属性应该建立索引。核心数据可能或可能不会定义另一个主键。

SQLite does maintain an implicit primary key, rowId, if one is not defined by the client code. But if the client code defines an integer primary key, SQLite will use that as its rowId. In other words, there is no advantage in space or time to try to be clever and use SQLite's rowId. Note that you can tell Core Data that your property should be indexed. Core Data may or may not be defining another primary key.

尝试依赖于Core Data如何实现其持久存储是 ** BAAAAD ** :您的代码将破坏与其他商店(过去或未来)的兼容性,并且可能会在未来更新时中断。

Trying to depend on how Core Data implements its persistent store is a **BAAAAD** idea: your code will break compatibility with other stores, past present or future, and will likely break with a future update.

您可能已经注意到Core Data现在支持与Mac OS X Lion或iOS 5的有序关系。底层实现最可能使用特定的附加列来存储排序信息。

You may have noticed that Core Data now supports ordered relationships with Mac OS X Lion or iOS 5. The underlying implementation most probably uses a specific additional column to store ordering information.

这篇关于有序CoreData记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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