NSFetchedResultsController和UITableView顯示CoreData的數據時用relationship分組的方法


使用NSFetchedResultsController和UITableView顯示CoreData的數據時,如果用relationship作為分組的關鍵字。比如Contact和Group兩個實例如下圖:

在顯示的時候,創建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();
}

 

當顯示出來以后,如果添加一行新的數據到Contact中,並且屬於一個已經存在的組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將會新建一個分組,並且其中只有一行數據。重新啟動程序的時候這一行又會顯示在GA分組中,那兩句代碼就是解決方案。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM