提问者:小点点

子级上的Firebase子级侦听器已移动


我正在尝试通过在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)
            
        
    }
    
   
    }

共1个答案

匿名用户

您的.ChildMoved侦听器将使用已移动的DataSnapShotPreviousSiblingKey调用,后者是需要在Friends列表中重新插入快照的项的键。 由于您的代码没有使用PreviousSiblingKey,因此它没有足够的信息来正确处理移动。

请参阅Firebase文档:https://Firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/classes/databasequery#observe_:andPreviousSiblingKeyWith:WithCancel: