NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方 ...
2013-01-18 18:06 三戒1993 阅读(110) 评论(0) 编辑 收藏 举报使用NSFetchedResultsController和UITableView显示CoreData的数据时,如果用relationship作为分组的关键字。比如Contact和Group两个实例如下图:
在显示的时候,创建NSFetchedResultsController
[代码]c#/cpp/oc代码:
01 |
/* |
02 |
Set
up the fetched results controller. |
03 |
*/ |
04 |
//
Create the fetch request for the entity. |
05 |
NSFetchRequest
*fetchRequest = [[NSFetchRequest alloc] init]; |
06 |
//
Edit the entity name as appropriate. |
07 |
NSEntityDescription
*entity = [NSEntityDescription entityForName: @"Contact" inManagedObjectContext:self.managedObjectContext]; |
08 |
[fetchRequest
setEntity:entity]; |
09 |
|
10 |
//
Set the batch size to a suitable number. |
11 |
[fetchRequest
setFetchBatchSize:20]; |
12 |
|
13 |
//
Edit the sort key as appropriate. |
14 |
//NSSortDescriptor
*sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"GName" ascending:NO]; |
15 |
//
NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"grp" ascending:YES comparator:^(id obja, id objb){ |
16 |
//
Group *aa = obja; |
17 |
//
Group *bb = objb; |
18 |
//
NSNumber *aaa = aa.gid; |
19 |
//
NSNumber *bbb = bb.gid; |
20 |
//
return [aaa compare:bbb]; |
21 |
//
}]; |
22 |
//
NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"Online" ascending:YES comparator:^(id obja, id objb){ |
23 |
//
NSNumber *aa = obja; |
24 |
//
NSNumber *bb = objb; |
25 |
//
return [aa compare:bb]; |
26 |
//
}]; |
27 |
|
28 |
//
NSArray *ary = nil; |
29 |
//
NSArray *a = [ary sortedArrayUsingComparator:^(id obja, id objb){ |
30 |
//
NSNumber *a = obja; |
31 |
//
NSNumber *b = objb; |
32 |
//
return [a compare:b]; |
33 |
//
}]; |
34 |
NSSortDescriptor
*sd1 = [NSSortDescriptor sortDescriptorWithKey: @"grp" ascending:YES
comparator:^(id obja, id objb){ |
35 |
Group
*ga = obja; |
36 |
Group
*gb = objb; |
37 |
return [ga.Name
compare:gb.Name]; |
38 |
}]; |
39 |
|
40 |
//NSSortDescriptor
*sd1 = [NSSortDescriptor sortDescriptorWithKey:@"GName" ascending:YES]; |
41 |
|
42 |
NSSortDescriptor
*sd2 = [NSSortDescriptor sortDescriptorWithKey: @"Online" ascending:YES]; |
43 |
NSArray
*sortDescriptors = [[NSArray alloc] initWithObjects:sd1, sd2, nil]; |
44 |
|
45 |
[fetchRequest
setSortDescriptors:sortDescriptors]; |
46 |
|
47 |
//
Edit the section name key path and cache name if appropriate. |
48 |
//
nil for section name key path means "no sections". |
49 |
NSFetchedResultsController
*aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath: @"grp" cacheName:nil]; |
50 |
aFetchedResultsController. delegate =
self; |
51 |
self.fetchedResultsController
= aFetchedResultsController; |
52 |
|
53 |
[aFetchedResultsController
release]; |
54 |
[fetchRequest
release]; |
55 |
//
[sortDescriptor release]; |
56 |
[sortDescriptors
release]; |
57 |
|
58 |
NSError
*error = nil; |
59 |
if (![fetchedResultsController_
performFetch:&error]) { |
60 |
/* |
61 |
Replace
this implementation with code to handle the error appropriately. |
62 |
|
63 |
abort()
causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the
user to quit the application by pressing the Home button. |
64 |
*/ |
65 |
NSLog( @"Unresolved
error %@, %@" ,
error, [error userInfo]); |
66 |
abort(); |
67 |
} |
[代码]c#/cpp/oc代码:
01 |
NSFetchRequest
*fr = [[NSFetchRequest alloc] init]; |
02 |
|
03 |
NSEntityDescription
*en = [NSEntityDescription entityForName: @"Group" inManagedObjectContext:self.managedObjectContext]; |
04 |
NSPredicate
*p = [NSPredicate predicateWithFormat: @"Name=%@" , @"GA" ]; |
05 |
[fr
setEntity:en]; |
06 |
[fr
setPredicate:p]; |
07 |
NSArray
*ary = [self.managedObjectContext executeFetchRequest:fr error:nil]; |
08 |
if (ary.count>0)
{ |
09 |
Group
*g = [ary objectAtIndex:0]; |
10 |
|
11 |
Contact
*c = (Contact*)[NSEntityDescription insertNewObjectForEntityForName: @"Contact" inManagedObjectContext:self.managedObjectContext]; |
12 |
[self.managedObjectContext lock ]; |
13 |
c.Passport
= @"New
Cnt" ; |
14 |
c.GName
= g.Name; |
15 |
c.Online
= [NSNumber numberWithInt:3]; |
16 |
[g
addCntObject:c]; |
17 |
[self.managedObjectContext
save:nil]; |
18 |
[self.managedObjectContext
unlock]; |
19 |
|
20 |
//[self.fetchedResultsController
performFetch:nil]; |
21 |
//[self.tableView
reloadData]; |
22 |
} |