我現在有從領域收集的變化更新的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