2016-11-18 20 views
0

我現在有從領域收集的變化更新的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中複製相同的行爲。 謝謝。

回答

0

收集通知中包含移動操作是我們想要做的事情。我們在這裏追蹤功能:https://github.com/realm/realm-cocoa/issues/3571

從那個GitHub的問題:

好消息是,境界已經內部計算移動操作。

壞消息是,移動操作轉換爲插入和刪除在變化計算算法的盡頭:https://github.com/realm/realm-object-store/blob/28ac73d8881189ac0b6782a6a36f4893f326397f/src/impl/collection_change_builder.cpp#L35-L38

我依稀記得這個正在做,由於UITableView中的API不處理的移動操作非常漂亮,儘管結果非常有效,但他們會在某些情況下崩潰。由於插入/刪除對不會發生這種情況,並且此功能將用於在99%的時間內爲UITableView提供動力,所以我們選擇「扁平」動作來解決此問題。

+0

事實上,事實證明它已經爲你移動了一切。這非常方便。 – Ryan

+0

雖然太糟糕了,但他們不提供部分關鍵路徑。 – Ryan

+0

是的,分組在這裏被跟蹤:https://github.com/realm/realm-cocoa/issues/3384 – jpsim