【问题标题】:Firebase child listener on child moved孩子上的 Firebase 孩子监听器已移动
【发布时间】:2020-06-26 12:27:59
【问题描述】:

我正在尝试通过在 firebase child listener 中移动的 firebase child 重新排列列表中的项目。但是要移动的项目会替换新位置的项目。然而,当我上下滚动时,排序是不正确的

      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)
            
        
    }
    
   
    }

【问题讨论】:

    标签: ios swift firebase-realtime-database


    【解决方案1】:

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

    请参阅 Firebase 文档:https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DatabaseQuery#observe_:andprevioussiblingkeywith:withcancel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多