NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方 ...

使用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     }
当显示出来以后,如果添加一行新的数据到Contact中,并且属于一个已经存在的组GA,代码如下:

[代码]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     }
如果不加最后的两行。那么UITableView将会新建一个分组,并且其中只有一行数据。重新启动程序的时候这一行又会显示在GA分组中,那两句代码就是解决方案。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值