2016-11-18 39 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

相关问题