我正在尝试从另一个视图控制器编辑 nsfetchresults 记录,如果显示我的所有数据库条目,它工作正常。但是当我按日期过滤表格并尝试编辑它时,我收到了这个错误
2013-03-22 23:09:02.895 easyDB[1079:907] * -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070 2013-03-22 23:09: 18.865 easyDB[1079:907] 捕获异常!
NSInternalInconsistencyException
无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (6) 必须等于更新前该节中包含的行数 (2),加上或减去数字从该部分插入或删除的行数(0 插入,0 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。
我发送
NSIndexPath *indexpath = (NSIndexPath*)sender;
GlucoseDetailViewController *glucoseDetailsVC = (GlucoseDetailViewController*)segue.destinationViewController;
GlucoseRecords *rec = [self.fetchedResultsController objectAtIndexPath:indexpath];
[glucoseDetailsVC setSentRecord:rec];
在编辑视图中
[self.sentRecord setGlucose:[NSNumber numberWithInt:[[txtGlucoseReading text] intValue]]];
[self.sentRecord setDate:self.dateToAdd];
[self.sentRecord setMealType:[NSString stringWithString:self.strMealOption]];
[self.sentRecord setQuickNote:[NSString stringWithString:self.strQuickNote]];
[self.sentRecord setDeleted:[NSNumber numberWithBool:NO]];
[self.sentRecord setWhoRecorded:[self.settings currentUser]];
NSError *error;
if (![[self.settings managedObjectContext] save:&error])
NSLog(@"Failed to add default user with error: %@", [error domain]);
FetchedResults 控制器
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil && [self.strCurrentEntetyName isEqualToString:self.strEntetyName] &&(self.filteChanged==[NSNumber numberWithBool:NO])) {
return _fetchedResultsController;
}
self.filteChanged = [NSNumber numberWithBool:NO];
self.strCurrentEntetyName = [NSString stringWithString:self.strEntetyName];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[self strEntetyName]];
//NSEntityDescription *entity = [NSEntityDescription
// entityForName:@"Records" inManagedObjectContext:[self.settings managedObjectContext]];
if (self.isDateFilterSet==[NSNumber numberWithBool:NO])
{
fetchRequest.predicate = [NSPredicate predicateWithFormat:@" whoRecorded.username = %@ AND deleted=0", [self.settings.currentUser username]];
}
else
{
fetchRequest.predicate = [NSPredicate predicateWithFormat:@" (whoRecorded.username = %@) AND (deleted = 0) AND (date =>%@) AND (date<=%@)", [self.settings.currentUser username],self.dateMinSetFilter ,self.dateMaxSetFilter];
}
NSLog(@"User:%@",[self.settings.currentUser username]);
// [fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"date" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:100];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self.settings managedObjectContext] sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
我了解问题所在,但我看不到任何解决问题的好方法。我可以在编辑之前重置表格以显示所有条目,但我认为这不是一个好的解决方案。有没有更好的方法来解决这个问题?
谢谢