Firestore覆盖iOS Swift中已存在的数据 [英] Firestore overwrites already exist data in iOS Swift

查看:71
本文介绍了Firestore覆盖iOS Swift中已存在的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

HoDYPGk4wvhrlgvA3bRYPAromE22 is currently logged in user. In firestore db i have followers and following tables. If current user taps on following button it writes in following table - current user is document id and then following user id with boolean true.

以下是下表的屏幕截图。

Here is the screenshot of following table.

当用户点击下面的按钮时,我的问题是覆盖ID后面的6wP1l0yBDDQA146NqNBBzFEgX4u2。它没有添加新ID。

My issues when user taps on following button it overwrites 6wP1l0yBDDQA146NqNBBzFEgX4u2 following id. it does not add has new 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])

if给合并:在setData之后为true,如setDfata([id:true],merge:true),它表示方法重载参数。

if give merge: true in after setData like setDfata([id:true], merge: true) it says method overloads argument.

我试过这样但不太合适:

I tried like this but does not too work:

     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)

 }


推荐答案

通常我会在这里使用自动递增键。 关注 - > AutoKey - > [followBy:userId1,followTo:userId2] 因为Firestore支持查询,您可以通过这些查询轻松过滤用户跟随的用户。但根据注释和您当前的数据库结构,您需要在更新之前获取以前的数据,否则它将替换旧数据。见下面的例子:

Generally I will use Auto incremented key here. Following -> AutoKey -> ["followedBy": userId1, followedTo: userId2] because Firestore supports queries by which you can easily filter the users followed by someone. But based on comments and your current DB structure you need to fetch the previous data before updating it else it will replace the old data. See below example:

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

这篇关于Firestore覆盖iOS Swift中已存在的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆