简体   繁体   English

核心数据按两个描述符排序

[英]Core data sort data by two Descriptor

I have used core data in my ios app. 我在ios应用程序中使用了核心数据。 Now i have one table in which there are two columns . 现在我有一张表,其中有两列。

  1. Category 类别

  2. Order (In which there are NSNumber 1, 2 ,5) 顺序(其中有NSNumber 1、2、5)

I want to fetch data so It will first sort alphabetically by Category name and than by Order Number. 我想获取数据,因此它将首先按类别名称而不是订单号的字母顺序进行排序。

I have used below code : 我用下面的代码:

NSEntityDescription *entity_Maintness = [NSEntityDescription
                                             entityForName:@"Maintness" inManagedObjectContext:__managedObjectContext];
    [fetchRequest_Maintness setEntity:entity_Maintness];


    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serialNumber" ascending:NO];


    NSArray *sortDescriptors12 = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor, nil];
    [fetchRequest_Maintness setSortDescriptors:sortDescriptors12];

But the data are sorted only by Category not by Serial Number. 但是数据仅按类别而不是序列号排序。

Thanks for Help. 感谢帮助。

The code above looks ok. 上面的代码看起来还可以。 Maybe there is an issue elsewhere in your code? 也许您的代码中的其他地方有问题? Here is some code I quickly generated for you that may serve as starting point?? 这是我快速为您生成的一些代码,可以用作起点?

#import "MainViewController.h"
#import "Maintness.h"

@interface MainViewController ()
@property (weak, nonatomic) IBOutlet UITextField *type;
@property (weak, nonatomic) IBOutlet UITextField *serialNumber;
@property (strong, nonatomic) NSManagedObjectContext *context;
- (IBAction)addButtonPressed:(id)sender;
- (IBAction)retrieveButtonPressed:(id)sender;

@end

@implementation MainViewController

#pragma mark - lazy instantiation
- (NSManagedObjectContext *)context{
    if (!_context){
        id appDelegate = (id)[[UIApplication sharedApplication] delegate];
        _context = [appDelegate managedObjectContext];
    }
    return _context;
}

#pragma mark - core data interactions
- (IBAction)addButtonPressed:(id)sender {
    NSError *error = nil;

    Maintness *newMaintness = nil;

    newMaintness = [NSEntityDescription insertNewObjectForEntityForName:@"Maintness" inManagedObjectContext:self.context];

    newMaintness.type = self.type.text;
    newMaintness.serialNumber = self.serialNumber.text;

    if (![self.context save:&error]) {
        NSLog(@"Oh no - error: %@", [error localizedDescription]);
    } else {
        NSLog(@"It appears the details were added ok");
    }

}

- (IBAction)retrieveButtonPressed:(id)sender {
    NSError *error = nil;
    // Set up fetch
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    // Set up entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Maintness" inManagedObjectContext:self.context];
    [fetch setEntity:entity];
    // Set up sorting
    //   - sorts in order of array
    NSSortDescriptor *primarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
    NSSortDescriptor *secondarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serialNumber" ascending:NO];
    NSArray *sortDescriptors = @[primarySortDescriptor, secondarySortDescriptor];
    [fetch setSortDescriptors:sortDescriptors];

    NSArray *results = [self.context executeFetchRequest:fetch error:&error];

    for (Maintness *result in results) {
        NSLog(@"type: %@ serial number: %@", result.type, result.serialNumber);
    }
}
@end

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM