【问题标题】:Duplicates child node when setting a value Firebase设置值 Firebase 时重复子节点
【发布时间】:2020-02-13 22:06:21
【问题描述】:

过去几天我一直在尝试解决这个问题,但结果还是一样。当用户点击添加好友时,会将好友的 id 作为节点添加到“好友”下(如预期的那样);但是,当我尝试更新或设置该 id 的值时,它会再次将 id 与该值一起添加,而不是仅将值添加到现有子项。我如何制作它以便添加它而不添加整个其他孩子?

添加好友的代码如下:

 func saveFriend(selectedFriend: FUser) {

    if friendId.contains(selectedFriend.objectId as String) {

        return
    }

    //get current friends
    var currentFriends = FUser.currentUser()!.friends

    currentFriends.append(selectedFriend.objectId)

    let newDate = dateFormatter().string(from: Date())
    let secondsDate: Int = Int(Date().timeIntervalSince1970)


    updateUser(withValues: [kFRIEND : currentFriends, kUPDATEDAT : newDate, kSECONDSDATEUPDATED : secondsDate]) { (success) in

        self.loadFriends()
        NotificationCenter.default.post(name: .addedFriend, object: nil)

    }


}

这是添加值的代码(将 updateChildValues 注释掉,我也尝试过并得到相同的结果):

func increaseFriendshipValue(increase: Int) {
    let friend = withUsers.first!.objectId
    let friendValueRef = firebase.child(kUSER).child(FUser.currentId()).child(kFRIEND).child(friend)

   // friendValueRef.updateChildValues([friend: +increase])

    friendValueRef.setValue(increase)


}

编辑:这是我目前在好友视图中的内容,我从好友添加到数组的方式加载好友(没有值):

  @objc func loadFriends() {
    cleanup()

    let friendIds = FUser.currentUser()!.friends

    if friendIds.count > 0 {

        for friendId in friendIds {

            firebase.child(kUSER).queryOrdered(byChild: kOBJECTID).queryEqual(toValue: friendId).observeSingleEvent(of: .value) { (snapshot) in

                if snapshot.exists() {
                    let userDictionary = ((snapshot.value as! NSDictionary).allValues as Array).first

                    let fuser = FUser.init(_dictionary: userDictionary as! NSDictionary)

                    self.friends.append(fuser)
                    self.friendId.append(fuser.objectId)
                    self.friends.sort(by: {$0.username < $1.username})
                }
                self.tableView.reloadData()

            }

        }

    } else {
        ProgressHUD.showError("You currently have no friends saved. Please unlock some")
    }

}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    您正在以两种不同的格式写入数据。

    首先在saveFriend,你是这样写的:

    "friends": {
      "0": "uidOfTheUser",
      "1": "uidOfAnotherUser"
    }
    

    然后在increaseFriendshipValue 中,您将其更新为:

    "friends": {
      "uidOfTheUser": 1,
      "uidOfAnotherUser": 0
    }
    

    由于您似乎实际上希望为每个用户保留一个计数,我将假设后者是正确的结构。这意味着您也需要在saveFriend 中以该格式写入用户。

    问题似乎出在:

    var currentFriends = FUser.currentUser()!.friends
    
    currentFriends.append(selectedFriend.objectId)
    

    在这里,您将selectedFriend.objectId 添加到一个数组中,这意味着 Swift 会生成一个新的数组元素:上面第一个 JSON sn-p 中的 01

    您实际上想要做的是直接写入数据库,类似于您在increaseFriendshipValue 中所做的。假设初始计数为 0,则类似于:

    let friendsRef = firebase.child(kUSER).child(FUser.currentId()).child(kFRIEND)
    
    let newFriendRef = child(selectedFriend.objectId)
    
    newFriendRef.setValue(0)
    

    需要注意的几点:

    1. 您确实应该是using a transaction 以增加朋友价值,否则来自多个用户的写入可能会覆盖彼此的结果。
    2. 现在saveFriend 似乎有覆盖朋友现有值的重大风险。您可能正在检查用户是否已经在代码中的某处成为朋友,但在这里您也需要使用事务。
    3. 如果您在这两种情况下都使用事务,您可能希望将创建“朋友”节点的代码和更新它的代码合并到一个函数中,因为它们看起来非常相似。李>

    【讨论】:

    • 我按照您的建议将其更改为 setValue(0) ,这在确保它们不被重复方面非常有效;但是,当我加载朋友时,我无法将他们加载到朋友视图中(我知道它当前设置为加载一个数组,但我不确定如何更改它以加载其他值)。此外,每次我打开应用程序时,它都会删除 Friends 的所有值。我该如何阻止这个?非常感谢您的帮助,我真的很感激!
    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2022-01-10
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多