【问题标题】:Remove value when listing firebase child node on tableViewCell在 tableViewCell 上列出 firebase 子节点时删除值
【发布时间】:2019-09-05 18:39:40
【问题描述】:

我正在尝试在表格视图中列出来自 firebase 节点的所有用户(这是有效的),但随后想从列表中删除当前用户(这无效)。

我尝试使用 removeValue(),但它仍然在表视图上与当前用户一起运行 - 我也不想从 firebase 中删除用户

然后我尝试使其仅在用户 autoId 不等于当前用户 ID 但仍显示在表格视图单元格上时才运行

希望有任何帮助:)

我的firebase结构是这样的:

"users" : {
   "8x1SGi2P0KVOKm4i60JQLP1bdU63" : {
     "fullname" : "bbbb",
     "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/pinion-4896b.appspot.com/o/profile_image%2F8x1SGi2P0KVOKm4i60JQLP1bdU63?alt=media&token=7932b14f-c2d8-46fd-9dd1-c607217fe8d3",
   },
   "B7rwHiCTlphZjKXfPSEzkIwl8RH2" : {
     "fullname" : "Aaaa ",
     "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/pinion-4896b.appspot.com/o/profile_image%2FB7rwHiCTlphZjKXfPSEzkIwl8RH2?alt=media&token=072e1f41-935e-430d-af99-dc640381f8e6",
   },
   "FRuuk20CHrhNlYIBmgN4TTz3Cxn1" : {
     "fullname" : "eeee",
     "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/pinion-4896b.appspot.com/o/profile_image%2FFRuuk20CHrhNlYIBmgN4TTz3Cxn1?alt=media&token=bf89b903-a51a-4d6d-bdef-fe2667d78842",
   },

列出用户的代码:

func observeUsers(completion: @escaping (UserModel) -> Void) {
    REF_USERS.observeSingleEvent(of: .childAdded, with: { snapshot in
        if let dict = snapshot.value as? [String: Any] {
            let user = UserModel.transformUser(dict: dict, key: snapshot.key)

            //line below used first to remove value from listing on table view
            //Api.User.REF_USERS.child("users").child(Api.User.CURRENT_USER!.uid).removeValue()

            //line below - if user autoID is not equal to the current user then list
            if user.id != Api.User.CURRENT_USER?.uid {
                completion(user)
            }
        }
    })
}

编辑:

func loadUsers() {

    var allUsers = [String]()

    Api.User.REF_USERS.observe(.value, with: { snapshot in
        for child in snapshot.children { //build the array of keys
            let snap = child as! DataSnapshot
            let key = snap.key
            allUsers.append(key)
            print(allUsers)
        }
    })
}

【问题讨论】:

    标签: ios swift firebase uitableview firebase-realtime-database


    【解决方案1】:

    而不是删除试试这个 创建表数据的模型 然后创建一个空数组 在获取数据时追加所有除了具有相同 currentuser.uid 的数据 然后重新加载表格视图,这将显示除当前用户之外的所有数据

    这是承诺的代码:

    但这需要在您的数据库中进行一些修改 让你的数据库像这样 “用户”:{ “childbyautid”:{ “全名”:“名称”, "profileimageurl": "你的网址", “用户标识”:“用户标识” }

    那么你可以这样写

    var myArr = [String]()
    Database.database().reference.child("users").observe(.value){(snapshot) in
    if snapshot.childcount > 1 { self.myArr.removeAll()
    for data in snapshot.children.allObjects as! [DataSnapshot]{
    if let d = data.value as? [string: any]{
    if Auth.auth.currentuser.uid != d["userid"]{
    myArr.append(d["name"]}else{print("this is the user itself"}}}
    

    【讨论】:

    • 感谢您的回复!我添加了一个编辑来显示创建一个数组并将所有用户添加到其中 - 你知道我如何在附加其他所有人的同时阻止当前用户被附加吗?
    • 我假设你得到的关键是你得到这样的用户 ID let x = Auth.auth.currentuser.uid 在附加到数组之前使用 if else 块来检查 key == x如果它是继续并且不追加,如果你愿意,我可以在明天给你代码
    【解决方案2】:

    您可以在 loadUsers 中通过添加 userId 检查来控制这一点,如下所示

    func loadUsers() {
    var allUsers = [String]()
    Api.User.REF_USERS.observe(.value, with: { snapshot in
        for child in snapshot.children { //build the array of keys
            let snap = child as! DataSnapshot
            let key = snap.key
    if key != currentUser.id{
            allUsers.append(key)
    }
            print(allUsers)
        }
    })
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多