[英]How to keep a cell selected when NSFetchedResultsChangeMove is called with 2 identical NSIndexPath?
If I click on a cell and a background Core Data refresh happens, my cell gets deselected. 如果我单击一个单元格,然后发生后台核心数据刷新,则取消选中我的单元格。
After investigation, my NSFetchedResultsControllerDelegate
's implementation of controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
gets called with a type of NSFetchedResultsChangeMove
but indexPath
and newIndexPath
are identical (ie same row/section values). 经过调查,我
NSFetchedResultsControllerDelegate
的执行controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
被称为有型的NSFetchedResultsChangeMove
但indexPath
和newIndexPath
完全相同(即同一行/段值)。
The NSFetchedResultsChangeUpdate
type doesn't get called. NSFetchedResultsChangeUpdate
类型不会被调用。
My implementation of controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
is the typical one provided in Apple's documentation. 我的
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
是Apple文档中提供的典型实现。 I believe that switching from: 我相信从:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
...
switch(type) {
...
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
to: 至:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
...
switch(type) {
...
case NSFetchedResultsChangeMove:
if (indexPath.section != newIndexPath.section || indexPath.row != newIndexPath.row) {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
}
}
would solve my problem. 会解决我的问题。
My question is: is this fix correct or do I miss something that could explain why my update is of the NSFetchedResultsChangeMove
type? 我的问题是:此修复程序正确吗?或者我错过了什么可以解释为什么我的更新是
NSFetchedResultsChangeMove
类型的事情吗?
My problem was that I was that I was assigning values to NSManagedObject
instances that were the same as before. 我的问题是我正在为与以前相同的
NSManagedObject
实例分配值。 ie, if foo.bar
was 2, I would call the API and call a new foo.bar = 2
assignment on my object. 即,如果
foo.bar
为2,我将调用API并在对象上调用新的foo.bar = 2
分配。 Even though, values were identical, Core Data was still considering the value had changed. 即使值是相同的,Core Data仍在考虑值已更改。
It is still weird that I got a NSFetchedResultsChangeMove
type instead of a NSFetchedResultsChangeUpdate
type but heh. 我还是得到了
NSFetchedResultsChangeMove
类型而不是NSFetchedResultsChangeUpdate
类型,但是还是很奇怪。
What I did to resolve this is: 为了解决这个问题,我要做的是:
if ([object.changedValues count] == 0) {
// Reverting changed on object
[context refreshObject:object mergeChanges:NO]
}
At the end of each update, rather than comparing attributes one by one. 在每次更新结束时,而不是一个一个地比较属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.