我有一個父類,處理所有的UITableview持久性數據管理和UITableView行managemetn。我已經將一個新的XCode4項目的大部分代碼複製到UITableview的持久數據中。現在我試圖刪除子類中的一個表格,並且在刪除一行時沒有任何委託方法被調用,導致我的tableview處於不一致的狀態。iPhone4 iOS5 NSFetchedResultsController如何在一個子類中正確設置委託?
我是否正確地指派委託?在這種情況下誰是NSFetechedResultsController的委託人? 我是否需要明確地讓我的子類成爲NSFetchedResultsControllerDelegate?
下面是我如何檢索NSFetchedResultsController,請注意它如何將自我指派爲委託。
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
/*
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:entityName inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:fetchBatchSize];
// Edit the sort key as appropriate.
// NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startMinute" ascending:YES];
NSArray *sortDescriptors;
if(sortDescriptor2!=nil)
{
sortDescriptors= [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor2, nil];
}else
{
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
}
[fetchRequest setSortDescriptors:sortDescriptors];
if(filterPredicate != nil)
{
[fetchRequest setPredicate:filterPredicate];
}
// 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:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.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();
}
return __fetchedResultsController;
}
當它的時間來刪除表中的行,沒有的委託方法被調用,甚至想到了錶行是真正刪除。爲了調試問題,我添加了以下2個斷言。第二個斷言失敗。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
id delegate = self.fetchedResultsController.delegate;
NSAssert(delegate!=nil,@"Tableview is not delegate of fetched results controller!");
//this assertion fails
NSAssert([((UITableViewController*)self) isEqual:delegate],@"Tableview is not delegate of fetched results controller!");
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context save:&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();
}
}
}
謝謝!