简体   繁体   English

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

[英]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 ,例如, 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. 由于setSortDescriptors:设置了一个排序描述符数组,因此您可以指定要排序的多个键。 eg specify to order against name and salary . 例如,指定对namesalary进行排序。 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.

相关问题 在Core Data中存储用于绘制图形的数据集的最佳方法是什么? - What is the best way to store data sets for graphing in Core Data? 删除Core Data中实体中所有实例的最有效方法是什么? - What is the most efficient way to remove all instances in an entity in Core Data? 限制核心数据存储大小的最佳方法是什么? - What's the best way to limit the size of a Core Data store? 首次启动时填写数据库核心数据的最佳方法是什么 - What is the best way fill in database core data at first launch 按相关实体的属性对核心数据集进行排序 - Sort a Core Data set by attribute on a related entity iOS /核心数据:检查核心数据中属性是否有值的最佳方法是什么? - iOS/core-data: What is best way to check if there is a value for an attribute in core data? 将JSON数据存储到Core Data的最佳方法 - Best way to store JSON data in to Core Data 使用Swift 3实现短路(对单个数据进行多次操作)并用核心数据过滤的最佳方法是什么? - What is the best way to implement shorting(Multiple time on single data) and filter with core data with Swift 3? 在iPhone Core Data或SQLite中存储长期数据的最佳方法是什么? - What is best way to store long term data in iPhone Core Data or SQLite? 从核心数据实体获取属性值数组的有效方法是什么? - What is an efficient way to get an array of property values from a core data entity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM