什么是排序核心数据实体的最好方法? [英] What is the best way to sort a Core Data Entity?

查看:164
本文介绍了什么是排序核心数据实体的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完全工作的核心数据模型,但是当我回到使用读取请求的数据,它在一个看似随机的顺序。什么是排序此数据的最佳方式?它是用在核心数据模型另一个表,和'查询'的第一个?或者,会是将数据下拉到一个数组和排序这样的说法?

I have a fully working core data model, but when I return the data using a fetch request, it's in a seemingly random order. What is the best way to sort this data? Is it to use another table in the Core Data model, and 'query' the first? Or would it be to pull down the data into an array, and sort it that way?

我不太知道怎么做任何这些,这是我问这个问题的原因。

I'm not too sure how to do either of these, which is the reason that I am asking this question.

推荐答案

您的问题是相当普遍的,但我会尽力给你一些提示。

Your question is quite general but I'll try to give you some hints.

当您使用 NSFetchRequest 类,您可以指定排序描述符。

When you use NSFetchRequest class you can specify sort descriptors.

从苹果DOC:

排序描述符的数组(实例 NSSortDescriptor )的
  指定如何返回的对象应该通过排序,例如
  姓氏然后通过名字。

An array of sort descriptors (instances of NSSortDescriptor) that specify how the returned objects should be ordered, for example by last name then by first name.

在这里,你可以找到在<一个简单的例子href=\"http://developer.apple.com/library/mac/#documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/fetching.html\">Core数据片段DOC

Here you can find a simple example within Core Data Snippets doc

NSManagedObjectContext *context = <#Get the context#>;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>"
    inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>"
    ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    // Handle the error
}

// release allocated objects if you don't use ARC

其中,

&LT; #Entity名称#> 是要检索的实体的名称,例如管理​​

<#Entity name#> is the name of the entity you want to retrieve, e.g. Manager.

&LT; #Sort键#> 是请求将使用命令,例如键的名称名称管理​​实体。

<#Sort key#> is the name of the key the request will use to order, e.g. name is an attribute of Manager entity.

所以,在我的例子:

NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name"
    ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByName, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

由于 setSortDescriptors:设置排序描述符的数组,可以指定多个密钥对您要订购。例如指定命令对名称薪水。排序描述符在它们出现在sortDescriptors数组中的顺序应用。所以,在我的例子,首先由名称,然后通过薪水

Since setSortDescriptors: sets an array of sort descriptors, you can specify multiple keys against you want to order. e.g. specify to order against name and salary. The sort descriptors are applied in the order in which they appear in the sortDescriptors array. So, in my example, first by name and then by salary.

所以,在我的例子,它可能成为

So, in my example, it could become

NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name"
    ascending:YES];
NSSortDescriptor *sortBySalary = [[NSSortDescriptor alloc] initWithKey:@"salary"
    ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByName, sortBySalary, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

希望有所帮助。

修改

由于 fetchedObjects 包含 NSManagedObject (我想你没有改变你的请求的结果类型),你需要重复这样的:

Since fetchedObjects contains NSManagedObject (I suppose you did not change the result type of your request) you need to iterate like the following:

for(NSManagedObject* currentObj in fetchedObjects) {

    NSLog(@"Print the name %@", [currentObj valueForKey:@"name"]);
}

所以,你需要通过键值编码访问属性或关系。为了使您的生活更轻松,你能想到为你创建像<一个创建的实体 NSManagedObject 小类href=\"http://blog.chrismiles.info/2011/06/organising-core-data-for-ios.html\">organising-core-data-for-ios.

So, you need to access attributes or relationships through Key Value Coding. To make your life easier you could think to create NSManagedObject subclasses for the entities you created like organising-core-data-for-ios.

这篇关于什么是排序核心数据实体的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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