Ios 已移动子节点上的Firebase子节点侦听器

Ios 已移动子节点上的Firebase子节点侦听器,ios,swift,firebase-realtime-database,Ios,Swift,Firebase Realtime Database,我试图通过在firebase子侦听器中移动的firebase子对象重新排列列表中的项目。但要移动的项目将替换位于新位置的项目。但是,当我上下滚动时,排序不正确 query.observe(.childMoved) { dataSnapshot in if let value = dataSnapshot.value as? [String: Any], let index = self.friends.index(where: {$0.fuid

我试图通过在firebase子侦听器中移动的firebase子对象重新排列列表中的项目。但要移动的项目将替换位于新位置的项目。但是,当我上下滚动时,排序不正确

      query.observe(.childMoved) { dataSnapshot in
        if let value = dataSnapshot.value as? [String: Any],
            let index = self.friends.index(where: {$0.fuid == dataSnapshot.key}) {
            let indexPath = IndexPath(item: index, section: 0)
            
            let movedPost = friendsModel(snapshot: dataSnapshot)
            
            self.friends.remove(at: index)
            self.friends.insert(movedPost, at: 0)
            
            self.tableView.reloadRows(at: [IndexPath(item: 0, section: 0)], with: .none)
            self.tableView.reloadRows(at: [indexPath], with: .none)
            
        
    }
    
   
    }

您的
.childMoved
侦听器将使用移动的
DataSnapshot
previousSiblingKey
调用,该键是
之后的项的键,您需要在
好友列表中重新插入快照。由于您的代码未使用
previousSiblingKey
,因此它没有足够的信息来正确处理移动

请参阅Firebase文档: