繁体   English   中英

对具有多个值的核心数据进行排序

[英]Sorting in Core Data with Multiple Values

我在核心数据中存储了品牌列表,每个品牌都与多个内容相关联。 内容具有一个称为downStatus的标志,该标志用于指示是否下载了内容。 以下方法用于从按品牌名称排序的Core数据中获取所有品牌

-(void)getDownloadedBrands{

AppDelegate *aAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

NSMutableArray *aBrands = [[NSMutableArray alloc] init];


NSEntityDescription *entity =
[NSEntityDescription entityForName:@"Brand" inManagedObjectContext:aAppDelegate.managedobjectcontext];
 NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"brandName" ascending:YES];
//NSSortDescriptor *sort1=[[NSSortDescriptor alloc]initWithKey:@"downStatus" ascending:YES];

[request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];

aBrands =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request error:nil];
[request release];
[sort release];
NSLog(@"%@",aBrands);

brands = [[NSMutableArray alloc]initWithArray:aBrands];

aAppDelegate.dbBrandArr = brands;

[self loadGridView];

}

现在,我想使用Content中的downStatus进行排序。 因此,就像按字母顺序下载品牌,然后按字母顺序下载未下载品牌。 downStatus取两个值1表示已下载), 0表示未下载。 请帮忙。

一种方法是使用1/0 on downStatus属性值为1/0 on downStatusNSPredicate并使用相同的字母sortDescriptor向CoreData发出2 fetchRequests

现在,通过将0数组添加到1数组来创建一个新的Array。 在您的代码中,它看起来像这样:

-(void)getDownloadedBrands{
    AppDelegate *aAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    //2 arrays
    NSMutableArray *aBrands0 = [[NSMutableArray alloc] init];
    NSMutableArray *aBrands1 = [[NSMutableArray alloc] init];
    //set entity en make requests
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Brand" inManagedObjectContext:aAppDelegate.managedobjectcontext];
    NSFetchRequest *request0 = [[NSFetchRequest alloc] init];
    NSFetchRequest *request1 = [[NSFetchRequest alloc] init];
    [request0 setEntity:entity];
    [request1 setEntity:entity];
    //create sortDescriptor alphabetically
    NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"brandName" ascending:YES];
    //create predicates on downStatus property
    NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"downStatus == %@", [NSNumber numberWithBool:0]];
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"downStatus == %@", [NSNumber numberWithBool:1]];
    //set predicates to the requests
    [request0 setPredicate:predicate0];
    [request1 setPredicate:predicate1];
    //set sortDescriptor to both requests
    [request0 setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    [request1 setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    //fetch arrays with downStatus 1/0
    aBrands0 =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request0 error:nil];
    aBrands1 =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request1 error:nil];
    //release requests // NOT USING ARC??
    [request0 release];
    [request1 release];
    [sort release];
    //log results
    NSLog(@"aBrands0: %@",aBrands0);
    NSLog(@"aBrands1: %@",aBrands1);
    //add object 0 array to 1 array
    NSArray *combinedArray = [aBrands1 arrayByAddingObjectsFromArray:aBrands0];
    //copy array to brands
    brands = [[NSMutableArray alloc]initWithArray:combinedArray];
    //set property on appDelegate
    aAppDelegate.dbBrandArr = brands;
    //reload tableView
    [self loadGridView];
}

-编辑,使用downStatus键为NSDictionary添加答案

-(void)getDownloadedBrands{
    AppDelegate *aAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    NSMutableArray *aBrands = [[NSMutableArray alloc] init];


    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Brand" inManagedObjectContext:aAppDelegate.managedobjectcontext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"brandName" ascending:YES];
    //NSSortDescriptor *sort1=[[NSSortDescriptor alloc]initWithKey:@"downStatus" ascending:YES];

    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];

    aBrands =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request error:nil];

    NSMutableArray *aBrands0 = [[NSMutableArray alloc] init];
    NSMutableArray *aBrands1 = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in aBrands) {
        if ([dict valueForKeyPath:@"downStatus"] == YES)
        {
            //add to 1 array
            [aBrands1 addObject:dict];
        }
        else
        {
            //add to 0 array
            [aBrands0 addObject:dict];
        }
    }

    //sort arrays
    NSArray * array1 = [aBrands1 sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    NSArray * array0 = [aBrands0 sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    NSArray * allArray = [aBrands1 arrayByAddingObjectsFromArray:aBrands0];

    //combine arrays

    //copy array to brands
    brands = [[NSMutableArray alloc]initWithArray:combinedArray];
    //set property on appDelegate
    aAppDelegate.dbBrandArr = brands;
    //reload tableView
    [self loadGridView];

    [request release];
    [sort release];
    NSLog(@"%@",aBrands);
}

如果计划在UITableViewController中使用获取结果,则可以将fetchedResultsController的sectionNameKeyPath用作downStatus,并保留品牌名称的排序描述符。 可以看起来像这样:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"customSectionTitle" cacheName:nil];

将其放在您的NSManagedObject的头文件中:

-(NSString*)customSectionTitle;

而这在.m中:

-(NSString*)customSectionTitle{
    if ([downStatus boolValue] == YES) {
        return @"Downloaded";
    }
    else {return @"Not Downloaded";}
}

如果您不打算使用fetchedResultsController,则只需先按downStatus排序,再按品牌名称排序。

暂无
暂无

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

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