iPhone Core数据提取主键 [英] iPhone Core Data Fetch Primary Key

查看:262
本文介绍了iPhone Core数据提取主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



因此,在 .sqlite 文件中,它管理主键本身。



主键属性名称为 Z_PK



我的问题是,表格按主键升序排列。



我正在使用该代码

  NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@_ PKascending:YES]; 
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

但不起作用。



我使用_PK的titleinsted,它工作正常

  NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey :@titleascending:YES]; 
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

那么,使用Table的主键有什么不同的方法吗?


< CoreData管理持久化对象图。它不是RDBM。您无法控制唯一主键,因此您无法控制其值。这只是一个实现细节,不应该因任何原因引用。



因此,为什么要根据它进行排序?我可以想到几个原因...


  1. 您要按照它们添加到数据库的顺序访问对象。但是,您可以通过多种方式轻松完成此操作。

1a。您可以向每个实体添加字段。您可以将其设置为UUID,也可以将其设置为递增整数。



1b。你可以有一个实体,例如EntityList,它与Entity有一对多的关系。



1c。您可以从Entity向自己添加一对一关系,然后像管理链接列表一样管理它。每次添加一个Entity时,将它放在列表的末尾。



这两个解决方案中的任何一个都让您能够按照添加的顺序访问Entity对象。


  1. 或者,你真的不在乎你看到的顺序,但你想要一致的顺序。

2a。添加primaryKey属性,并为其分配一个UUID。



2b。使用有序对多关系。



最后,如果你真的想要基于主键的访问,我建议有一个单独的sqlite数据库,你用于特殊键访问。然后,您可以将一个键与CoreData存储中对象的objectID相关联。


I am developing an application that uses CoreData.

So in .sqlite file, It manages Primary key itself.

Primary key attribute name is Z_PK.

My problem is that, I wants data from my table sorted ascending by primary key.

I'm using that code

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"_PK" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

But it's not working.

when I'm using "title" insted of "_PK", It's working Fine

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

So is there any different way to use Table's Primary key?

解决方案

CoreData manages persistent object graphs. It is not a RDBM. You have no control over the unique primary key, so you have no control over its value. It is merely an implementation detail, and should not be referenced for any reason.

Thus, why do you want to sort based on it? I can think of several reasons...

  1. You want to access the objects in the order in which they were added to the database. However, you can easily do this in several ways.

1a. You can add a field to each Entity. You can set it to be a UUID, or in increasing integer.

1b. You could have an entity, something like "EntityList" which has an ordered one-to-many relationship to "Entity."

1c. You could add a one-to-one relationship from Entity to itself, then just manage it like a linked list. Every time you add an Entity, put it at the end of the list.

Either of this solutions give you the ability to access the Entity objects in order they were added.

  1. Or, you really don't care the order you see them, but you want a consistent ordering.

2a. Add a primaryKey attribute, and assign it a UUID.

2b. Use the ordered to-many relationship.

Finally, if you really want primary-key-based access, I would suggest having a separate sqlite database that you use for special keyed access. You can then associate a key to the objectID of an object in the CoreData store.

这篇关于iPhone Core数据提取主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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