這篇文章假定你已經(jīng)大概熟悉CoreDate和MagicalRecord框架的使用方法了.
1.如何在獲取CoreDate數(shù)據(jù)進(jìn)行多次排序
在需求狗面前, 必須要具備一些額外的技能才能應(yīng)對自如. 比如他們要求一個數(shù)組里面要求根據(jù)時間倒序, 又要根據(jù)字母正序, 身為程序狗如何優(yōu)雅的應(yīng)對一些苛刻的排序要求.
CoreDate對于一個數(shù)據(jù)進(jìn)行多次排序你需要下面代碼:
NSFetchRequest *fr = [[NSFetchRequest alloc] init];
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:[NSManagedObjectContext defaultContext]];
[fr setEntity:ed];
NSSortDescriptor *sd = [NSSortDescriptor
sortDescriptorWithKey:@"date" ascending:NO];
//根據(jù)時間倒序
NSSortDescriptor *sn = [NSSortDescriptor
sortDescriptorWithKey:@"name" ascending:YES];
//根據(jù)名字正序
[fr setSortDescriptors:@[sd,sn]];
NSError *error = nil;
NSArray *result = [[NSManagedObjectContext defaultContext] executeFetchRequest:fr error:&error];
MagicalRecord對與一個數(shù)據(jù)進(jìn)行多次排序你只需要一行代碼:
NSArray *result = [Note findAllSortedBy:@"date:NO,name" ascending:YES];
話不多說,高下立判
未完待續(xù)...
參考文獻(xiàn):
MagicalRecord Fetching 方法