[英]What is the best way to sort a Core Data Entity?
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? 是在Core Data模型中使用另一个表,还是'查询'第一个? 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. 你的问题很普遍,但我会试着给你一些提示。
When you use NSFetchRequest
class you can specify sort descriptors. 使用NSFetchRequest
类时,可以指定排序描述符。
From Apple doc: 来自Apple doc:
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. 一组排序描述符(NSSortDescriptor
实例),用于指定返回对象的排序方式,例如按姓氏,然后按名字排序。
Here you can find a simple example within Core Data Snippets doc 在这里,您可以在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
Where 哪里
<#Entity name#> is the name of the entity you want to retrieve, eg Manager
. <#Entity name#>是您要检索的实体的名称,例如Manager
。
<#Sort key#> is the name of the key the request will use to order, eg name
is an attribute of Manager
entity. <#Sort key#>是请求将用于订购的密钥的name
,例如, name
是Manager
实体的属性。
So, in my example: 所以,在我的例子中:
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByName, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
Since setSortDescriptors:
sets an array of sort descriptors, you can specify multiple keys against you want to order. 由于setSortDescriptors:
设置了一个排序描述符数组,因此您可以指定要排序的多个键。 eg specify to order against name
and salary
. 例如,指定对name
和salary
进行排序。 The sort descriptors are applied in the order in which they appear in the sortDescriptors array. 排序描述符按它们在sortDescriptors数组中出现的顺序应用。 So, in my example, first by name
and then by salary
. 所以,在我的例子中,首先是name
,然后是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];
Hope that helps. 希望有所帮助。
Edit 编辑
Since fetchedObjects
contains NSManagedObject
(I suppose you did not change the result type of your request) you need to iterate like the following: 由于fetchedObjects
包含NSManagedObject
(我想你没有更改请求的结果类型),你需要像下面这样迭代:
for(NSManagedObject* currentObj in fetchedObjects) {
NSLog(@"Print the name %@", [currentObj valueForKey:@"name"]);
}
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 . 为了让您的生活更轻松,您可以考虑为您创建的实体创建NSManagedObject
子类,例如组织core-data-for-ios 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.