我正在使用 NSFetchrequestController 使用核心数据更新 tableview。当我删除行/部分时,我暂时将其删除状态并将其发送到带有标签的网络服务。
subtask.status = DELETED; //status = 4;
完整的方法在这里。
MainTableViewCell *cell = (MainTableViewCell*)btn.superview.superview;
self.selectedIndexPath = [self.tableView indexPathForCell:cell];
Task *tobeUpdatedTask;
[popover dismissPopoverAnimated:YES];
if (btn.titleLabel.tag == 8) { //Deleting Main Task/Section
NSUInteger index = [self.sectionViewIndexMapping indexOfObject:btn.superview];
if (index == NSNotFound){
[self showAlert:@"Error Occured! Unable to find index of the section. Please go back to refresh data." WithTitle:ALERT];
[self.tableView reloadData];
[self.roundedButtonMenu removeFromSuperview];
return;
}
if ([[self.krFetchedResultsController sections] count] <= index) {
[self showAlert:@"Error Occured! Unable to find index of the section. Please go back to refresh data." WithTitle:ALERT];
[self.tableView reloadData];
[self.roundedButtonMenu removeFromSuperview];
return;
}
id <KRFetchedResultsSectionInfo> si = [[self.krFetchedResultsController sections] objectAtIndex:index];
Task *task = (Task *)si.theManagedObject;
tobeUpdatedTask = task;
if ([task.status isEqualToString:NEW]) {
//[self.tableView beginUpdates];
[self.managedObjectContext deleteObject:si.theManagedObject];
}else {
task.status = DELETED;
for (SubTask *st in task.subTasks) {
st.status = DELETED;
}
}
}else { //Delete SubTask
//FIXME: deleting countinously will kill the app. dismissing popover might work.
if (self.selectedIndexPath == NULL) {
NSLog(@"Unable to find index subtask object. Oops");
return;
}
SubTask *subtask = [self.krFetchedResultsController objectAtIndexPath:self.selectedIndexPath];
tobeUpdatedTask = subtask.section;
if ([subtask.status isEqualToString:NEW]) {
[self.managedObjectContext deleteObject:[self.krFetchedResultsController objectAtIndexPath:self.selectedIndexPath]];
}else{
subtask.status = DELETED;
subtask.section.status = EDITED;
}
}
//Remove rounded-menu if exists
[self.roundedButtonMenu removeFromSuperview];
[self.overlayView removeFromSuperview];
[self refreshDatesAndPercentage:tobeUpdatedTask];
[self save];
[self createGanttViewData:nil];
[self.tableView reloadData];
根据网络服务响应,我通过特定的子任务 ID 获取该对象来永久删除它,如下面的代码片段。
NSArray *array = [self fetchObjectByEntityType:entityType byTaskID:[taskDic valueForKey:@"task_id"]]; // task_id from JSON dictionary
[self.managedObjectContext deleteObject:[array objectAtIndex:0]];
[self save];
然而,这有时可以正常工作,但大多数时候应用程序崩溃并在控制台中出现以下错误日志!
*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效的节数。更新后表视图中包含的节数(4)必须等于更新前表视图中包含的节数(4),加上或减去插入或删除的节数(0插入,1已删除)。
我知道这是由于没有正确更新 tableview 行/部分。但是我已经尝试了所有可能的方法来使事情正确。请告诉我我在这里可能缺少什么或我的方法!先感谢您。