fetchedResultsContext 并没有真正删除对象并导致 commitEditingStyle 中出现断言问题
我遇到了一个奇怪的问题,需要一些帮助。
我正在进行一个核心数据项目,尚未使用 fetchedResultsController,只是使用 fetchRequets 和数组来填充 zableviews。所以现在我决定改变并使用 FRC ...
到目前为止一切都非常简单...但是使用 commitEditingStyle 我从那时起就遇到了问题 - 删除行时我抛出了一个像这样的异常:
The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update.
最后我发现发现这是因为我要删除的对象仍然保留在 FRC 中...我在 liko 中放入了一些 NSLog 部分:
NSLog(@"Number before deleting: %i - deleting %@",[[fetchedResultsController fetchedObjects] count], [fetchedResultsController objectAtIndexPath:indexPath]);
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSLog(@"Number before saving: %i",[[fetchedResultsController fetchedObjects] count]);
NSError *error;
if (![context save:&error]) {
[NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]];
}
NSLog(@"Number after saving: %i",[[fetchedResultsController fetchedObjects] count]);
NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade];
结果是这样的:
2011-05-20 14:49:35.398 Nivellator[6000:207] Number before deleting: 3
2011-05-20 14:49:35.399 Nivellator[6000:207] Number before saving: 3
2011-05-20 14:49:35.404 Nivellator[6000:207] Number after saving: 3
当然,当我告诉 tableview 它将获得 3 行时,它会呕吐但只通过了两个...但是这里出了什么问题?
我的旧代码看起来像这样并且工作没有任何问题...
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.entityTableView] == YES) {
if (editingStyle != UITableViewCellEditingStyleDelete) {
return;
}
if ([self.entityArray count] <= indexPath.row) {
return;
}
Member *thisEntity = [self.entityArray objectAtIndex:indexPath.row];
[delegate.managedObjectContext deleteObject:thisEntity];
NSError *savingError = nil;
if ([delegate.managedObjectContext save:&savingError] == YES) {
// Remove the entity from the Array and delete the corresponding table cell with animation
//
[self.entityArray removeObject:thisEntity];
NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade];
} else {
/* Error handling missing */
}
}
}
好吧
最后它归结为 RTFM ...所以我通过更改代码使其部分工作,例如:
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.entityTableView] == YES) {
if (editingStyle != UITableViewCellEditingStyleDelete) {
return;
}
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error]) {
[NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]];
}
if ([fetchedResultsController performFetch:&error]) {
[tableView beginUpdates];
NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
} else {
[NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]];
}
}
}
但是当我在 FRC 中使用段时我仍然遇到同样的错误...我找不到比这与 FRC 有关的更多内容...
有什么想法吗?
I am having a weird issue and need some help.
I am on a core data project and did not yet use a fetchedResultsController, just working with fetchRequets and arrays to populate zableviews. So now I decided to change and make use of FRC ...
Everything was pretty easy so far ... but with commitEditingStyle I am having issues since then - when deleting rows I am thrown an exception like this one:
The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update.
In the end I found out that it is because the object I want to delete remains in FRC ... I put some NSLog sections in liko so:
NSLog(@"Number before deleting: %i - deleting %@",[[fetchedResultsController fetchedObjects] count], [fetchedResultsController objectAtIndexPath:indexPath]);
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSLog(@"Number before saving: %i",[[fetchedResultsController fetchedObjects] count]);
NSError *error;
if (![context save:&error]) {
[NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]];
}
NSLog(@"Number after saving: %i",[[fetchedResultsController fetchedObjects] count]);
NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade];
The result was this:
2011-05-20 14:49:35.398 Nivellator[6000:207] Number before deleting: 3
2011-05-20 14:49:35.399 Nivellator[6000:207] Number before saving: 3
2011-05-20 14:49:35.404 Nivellator[6000:207] Number after saving: 3
Of course it will puke when I'm telling the tableview that it'll get 3 rows but only pass two ... but what's wrong here?
My old code was looking like this and worked without any issues ...
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.entityTableView] == YES) {
if (editingStyle != UITableViewCellEditingStyleDelete) {
return;
}
if ([self.entityArray count] <= indexPath.row) {
return;
}
Member *thisEntity = [self.entityArray objectAtIndex:indexPath.row];
[delegate.managedObjectContext deleteObject:thisEntity];
NSError *savingError = nil;
if ([delegate.managedObjectContext save:&savingError] == YES) {
// Remove the entity from the Array and delete the corresponding table cell with animation
//
[self.entityArray removeObject:thisEntity];
NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade];
} else {
/* Error handling missing */
}
}
}
OK
in the end it came down to RTFM ... so I made it partly work by changing the code such as:
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.entityTableView] == YES) {
if (editingStyle != UITableViewCellEditingStyleDelete) {
return;
}
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error]) {
[NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]];
}
if ([fetchedResultsController performFetch:&error]) {
[tableView beginUpdates];
NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath];
[tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
} else {
[NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]];
}
}
}
But when I'm using sectments in my FRC I still get the same error ... I cannot find anything more than this to do with the FRC ...
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
beginUpdates
冻结 tableview。beginUpdates
before you run the FRC's delegate methods.