iphone coredata 获取请求排序
我正在使用核心数据并成功获取结果。我对核心数据有几个问题 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对数组进行排序如下所示:
initWithKey: 参数是您要排序的属性。如果您有多个要排序的键,则可以将它们作为列表添加到sortedArrayUsingDescriptors:参数中。该列表将按第一个描述符排序,然后每组按下一个描述符排序。
-担
A sort on the array you have looks like this:
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
不以任何方式保证订购。如果您关心顺序,则需要在实体中包含一个属性(例如,将其称为“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.