【发布时间】: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