xcode 对核心数据实体进行排序的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11600571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:58:32  来源:igfitidea点击:

What is the best way to sort a Core Data Entity?

iosxcodearrayscore-data

提问by Alex Godbehere

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?

我有一个完全可用的核心数据模型,但是当我使用 fetch 请求返回数据时,它的顺序似乎是随机的。对这些数据进行排序的最佳方法是什么?是否在 Core Data 模型中使用另一个表,并“查询”第一个?或者是将数据下拉到一个数组中,然后按这种方式排序?

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

我不太确定如何做这些,这就是我问这个问题的原因。

回答by Lorenzo B

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

你的问题很笼统,但我会尽量给你一些提示。

When you use NSFetchRequestclass you can specify sort descriptors.

当您使用NSFetchRequestclass 时,您可以指定排序描述符。

From 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, e.g. Manager.

<#Entity name#>是您要检索的实体的名称,例如Manager.

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

<#Sort key#>是请求将用于排序的键的名称,例如nameManager实体的属性。

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. e.g. specify to order against nameand salary. The sort descriptors are applied in the order in which they appear in the sortDescriptors array. So, in my example, first by nameand then by salary.

由于setSortDescriptors:设置了一个排序描述符数组,因此您可以根据要排序的顺序指定多个键。例如,指定针对name和订购salary。排序描述符按它们在 sortDescriptors 数组中出现的顺序应用。因此,在我的示例中,首先是 by name,然后是 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];

Hope that helps.

希望有帮助。

Edit

编辑

Since fetchedObjectscontains 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 NSManagedObjectsubclasses for the entities you created like organising-core-data-for-ios.

因此,您需要通过键值编码来访问属性或关系。为了让您的生活更轻松,您可以考虑为NSManagedObject您创建的实体创建子类,例如organising-core-data-for-ios