【问题标题】:Firestore overwrites already exist data in iOS SwiftFirestore 覆盖 iOS Swift 中已存在的数据
【发布时间】:2018-06-11 07:18:11
【问题描述】:

HoDYPGk4wvhrlgvA3bRYPAromE22 当前是登录用户。在firestore db中,我有关注者和下表。如果当前用户点击以下按钮,它会在下表中写入 - 当前用户是文档 ID,然后使用布尔值 true 跟随用户 ID。

这是下表的截图。

当用户点击以下按钮时,我的问题会覆盖 6wP1l0yBDDQA146NqNBBzFEgX4u2 以下 id。它没有添加有新的 id。

这是我尝试过的代码:

 self.db.collection("followers").document(id).setData([API.User.CURRENT_USER!.uid: true])
      self.db.collection("following").document(API.User.CURRENT_USER!.uid).setData([id: true])

如果在 setData 之后像 setDfata([id:true], merge: true) 给出 merge: true ,则表示方法重载参数。

我尝试过这样但不太奏效:

     func followAction(withUser id: String) {

    print("withuser:::\(id)")
    let docRef = db.collection("user-posts").document(id)
    print("id::::\(docRef)")

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {

            self.db.collection("followers").document(id).setData([API.User.CURRENT_USER!.uid: true])

             self.db.collection("following").document(API.User.CURRENT_USER!.uid).setData([id: true])

            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")

            self.db.collection("feed").document(API.User.CURRENT_USER!.uid).setData([document.documentID: true])

        } else {
            print("Document does not exist")
            self.db.collection("followers").document(id).updateData([API.User.CURRENT_USER!.uid: true])

            self.db.collection("following").document(API.User.CURRENT_USER!.uid).updateData([id: true])
        }
    }

 //        self.db.collection("followers").document(id).setData([API.User.CURRENT_USER!.uid: true])
  //        self.db.collection("following").document(API.User.CURRENT_USER!.uid).setData([id: true])

  //        self.db.collection("following").document(API.User.CURRENT_USER!.uid).setData([id: true])
 // REF_FOLLOWERS.child(id).child(Api.User.CURRENT_USER!.uid).setValue(true)
 // REF_FOLLOWING.child(Api.User.CURRENT_USER!.uid).child(id).setValue(true)

 }

【问题讨论】:

  • 正如我在之前的回答中所说的,它是单个文档的字典,因此无论何时编写它都会用新字典替换整个字典。您需要使用自动生成的密钥,它应该像 - following -> userId -> autoGeneratedKey -> [followingUserId: userId1], autoGeneratedKey -> [followingUserId: userId2]。希望你明白了。 !!
  • @TheTiger 不,实际上我想只使用 uid 而不是自动生成的密钥
  • @TheTiger 嘿任何更新..
  • 那么你需要更新整个字典,而不仅仅是新的键值。您有 HoDYPGk4wvhrlgvA3bRYPAromE22 用户的数据,将新数据添加到此字典并保存。
  • 这里有任何示例代码..

标签: ios swift xcode firebase google-cloud-firestore


【解决方案1】:

一般我会在这里使用自动递增键。 Following -> AutoKey -> ["followedBy": userId1, followedTo: userId2] 因为 Firestore 支持查询,您可以通过这些查询轻松过滤某人关注的用户。但是根据 cmets 和您当前的数据库结构,您需要在更新之前获取以前的数据,否则它将替换旧数据。见下例:

let followingRef = Firestore.firestore().collection("Following").document("HoDYPGk4wvhrlgvA3bRYPAromE22")
followingRef.getDocument { (snapshot, error) in
    guard let _snapshot = snapshot else {return}

    if !_snapshot.exists {
        /// First time following someone
        followingRef.setData(["6wP1l0yBDDQA146NqNBBzFEgX4u2": true])
        return
    }

    // For next time
    var data = _snapshot.data()
    data["6wP1l0yBDDQA146NqNBBzFEgX4u2"] = true
    followingRef.setData(data)
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2019-04-28
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多