iphone coredata 获取请求排序

发布于 2024-09-01 11:33:20 字数 557 浏览 8 评论 0原文

我正在使用核心数据并成功获取结果。我对核心数据有几个问题 1.当我添加一条记录时,它是添加在实体的末尾还是开头。 2.我使用以下代码来获取数据。数组正在填充所有记录。但它们的顺序与我将记录输入实体的顺序不同。为什么?使用默认排序的依据是什么?

    NSFetchRequest* allLatest = [[NSFetchRequest alloc] init];
[allLatest setEntity:[NSEntityDescription entityForName:@"Latest" inManagedObjectContext:[self managedObjectContext]]];

NSError* error = nil;
NSArray* records = [managedObjectContext executeFetchRequest:allLatest error:&error];
[allLatest release];

3.我输入记录的方式,1,2,3,4......... 一段时间后,我想删除我首先输入的记录(我的意思是最旧的数据)。比如删除最旧的两条记录。怎么做呢?

I'm using core data and fetching the results successfully. I've few questions regarding core data
1. When I add a record, will it be added at the end or at the start of entity.
2. I'm using following code to fetch the data. Array is being populated with all the records. But they are not in the same order as I entered records into entity. why? on what basis default sorting is used?

    NSFetchRequest* allLatest = [[NSFetchRequest alloc] init];
[allLatest setEntity:[NSEntityDescription entityForName:@"Latest" inManagedObjectContext:[self managedObjectContext]]];

NSError* error = nil;
NSArray* records = [managedObjectContext executeFetchRequest:allLatest error:&error];
[allLatest release];

3. The way that I enter the records, 1,2,3,4......... after some time, I want to delete the records that I entered first(i mean oldest data). Something like delete oldest two records. How to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2024-09-08 11:33:20

对数组进行排序如下所示:

NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"SortKey" ascending:YES] autorelease];
NSArray *sortedList = [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];

initWithKey: 参数是您要排序的属性。如果您有多个要排序的键,则可以将它们作为列表添加到sortedArrayUsingDescriptors:参数中。该列表将按第一个描述符排序,然后每组按下一个描述符排序。

-担

A sort on the array you have looks like this:

NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"SortKey" ascending:YES] autorelease];
NSArray *sortedList = [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];

The initWithKey: argument is the property you would like to sort on. If you have multiple keys to sort on, they can be added to the sortedArrayUsingDescriptors: argument as a list. The list will be sorted by the first descriptor, then each group by the next descriptors.

-dan

不知所踪 2024-09-08 11:33:20

不以任何方式保证订购。如果您关心顺序,则需要在实体中包含一个属性(例如,将其称为“sequenceNumber”),并在 NSFetchRequest 的排序描述符中使用该属性。

Ordering is not guaranteed in any way. If you care about order, you need to include an attribute in your entity (for example, call it "sequenceNumber") and use that in a sort descriptor with your NSFetchRequest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文