NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方法
生活随笔
收集整理的這篇文章主要介紹了
NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用NSFetchedResultsController和UITableView顯示CoreData的數(shù)據(jù)時(shí),如果用relationship作為分組的關(guān)鍵字。比如Contact和Group兩個(gè)實(shí)例如下圖:
在顯示的時(shí)候,創(chuàng)建NSFetchedResultsController
/*Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"GName" ascending:NO];
// NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"grp" ascending:YES comparator:^(id obja, id objb){
// Group *aa = obja;
// Group *bb = objb;
// NSNumber *aaa = aa.gid;
// NSNumber *bbb = bb.gid;
// return [aaa compare:bbb];
// }];
// NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"Online" ascending:YES comparator:^(id obja, id objb){
// NSNumber *aa = obja;
// NSNumber *bb = objb;
// return [aa compare:bb];
// }];
// NSArray *ary = nil;
// NSArray *a = [ary sortedArrayUsingComparator:^(id obja, id objb){
// NSNumber *a = obja;
// NSNumber *b = objb;
// return [a compare:b];
// }];
NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"grp" ascending:YES comparator:^(id obja, id objb){
Group *ga = obja;
Group *gb = objb;
return [ga.Name compare:gb.Name];
}];
//NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"GName" ascending:YES];
NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"Online" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sd1, sd2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"grp" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
// [sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
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.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
?
當(dāng)顯示出來(lái)以后,如果添加一行新的數(shù)據(jù)到Contact中,并且屬于一個(gè)已經(jīng)存在的組GA,代碼如下:
NSFetchRequest *fr = [[NSFetchRequest alloc] init];NSEntityDescription *en = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:self.managedObjectContext];
NSPredicate *p = [NSPredicate predicateWithFormat:@"Name=%@", @"GA"];
[fr setEntity:en];
[fr setPredicate:p];
NSArray *ary = [self.managedObjectContext executeFetchRequest:fr error:nil];
if (ary.count>0) {
Group *g = [ary objectAtIndex:0];
Contact *c = (Contact*)[NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
[self.managedObjectContext lock];
c.Passport = @"New Cnt";
c.GName = g.Name;
c.Online = [NSNumber numberWithInt:3];
[g addCntObject:c];
[self.managedObjectContext save:nil];
[self.managedObjectContext unlock];
//[self.fetchedResultsController performFetch:nil];
//[self.tableView reloadData];
}
?
如果不加最后的兩行。那么UITableView將會(huì)新建一個(gè)分組,并且其中只有一行數(shù)據(jù)。重新啟動(dòng)程序的時(shí)候這一行又會(huì)顯示在GA分組中,那兩句代碼就是解決方案。
總結(jié)
以上是生活随笔為你收集整理的NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四年才等来的一天,务必要立志,要下决心!
- 下一篇: 非常不错的sharepoint webp