我现在有从领域收集的变化更新的tableView的代码如下:是否领域有fetchResultsController的.NSFetchedResultsChangeMove相当于:
func updateUI(changes: RealmCollectionChange<Results<Task>>) {
switch changes {
case .Initial(_):
tableView.reloadData()
case .Update(_, let deletions, let insertions, let modifications):
tableView.beginUpdates()
if !(insertions.isEmpty) {
tableView.insertRowsAtIndexPaths(insertions.map {NSIndexPath(forRow: $0, inSection: 0)},
withRowAnimation: .Automatic)
}
if !(deletions.isEmpty) {
tableView.deleteRowsAtIndexPaths(deletions.map {NSIndexPath(forRow: $0, inSection: 0)},
withRowAnimation: .Automatic)
}
if !(modifications.isEmpty) {
tableView.reloadRowsAtIndexPaths(modifications.map {NSIndexPath(forRow: $0, inSection: 0)}, withRowAnimation: .Automatic)
}
tableView.endUpdates()
break
case .Error(let error):
print(error)
}
}
前有中使用的核心数据,而不是境界,fetchedResultsController有非常方便的方法NSFetchedResultsChangeMove
当我排序核心数据。如苹果文档中所示,当某些东西移动时,表格中的当前位置被删除,然后插入到新的位置(是的,我意识到它是客观的C,我的代码很快,但它是一个明显的例子)。
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
正如你可以从代码中看到,境界似乎都而是所朝的参数。由于我正在制作聊天应用程序,因此当我使用核心数据时,移动功能非常重要,我希望能够在Realm中复制相同的行为。 谢谢。
事实上,事实证明它已经为你移动了一切。这非常方便。 – Ryan
虽然太糟糕了,但他们不提供部分关键路径。 – Ryan
是的,分组在这里被跟踪:https://github.com/realm/realm-cocoa/issues/3384 – jpsim