如何在iOS swift中使用firestore获取用户的帖子数 [英] How to get post count of user using firestore in iOS swift
问题描述
我已将用户帖子、关注者和关注者从 firebase 迁移到 firestore.现在我已经迁移了帖子、关注者以及关注和发布,关注者也算.
I have migrated user post, followers and following from from firebase to firestore. Now i have migrated post, followers and following and post, followers count too.
这里是我从 firebase 迁移到 firestore 的代码.
Here the code i have migrated from firebase to firestore.
import Foundation
import FirebaseDatabase
import FirebaseFirestore
class FollowApi {
var REF_FOLLOWERS = Database.database().reference().child("followers")
var REF_FOLLOWING = Database.database().reference().child("following")
let db = Firestore.firestore()
func followAction(withUser id: String) {
let docRef = db.collection("user-posts").document(id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
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).setData([API.User.CURRENT_USER!.uid: true])
self.db.collection("following").document(API.User.CURRENT_USER!.uid).updateData([id: true])
}
func unFollowAction(withUser id: String) {
let docRef = db.collection("user-posts").document(id)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
self.db.collection("feed").document(API.User.CURRENT_USER!.uid).delete()
} else {
print("Document does not exist")
}
}
self.db.collection("followers").document(id).setData([API.User.CURRENT_USER!.uid: NSNull()])
self.db.collection("following").document(API.User.CURRENT_USER!.uid).setData([id: NSNull()])
}
func isFollowing(userId: String, completed: @escaping (Bool) -> Void) {
let docRef = db.collection("followers").document(userId)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
print("documnetData::::\(String(describing: document.data()))")
if let dataDescription = document.data(), let _ = dataDescription[API.User.CURRENT_USER!.uid] as? Bool {
completed(true)
}
completed(false)
} else {
completed(false)
}
}
}
func fetchCountFollowing(userId: String, completion: @escaping (Int) -> Void) {
// REF_FOLLOWING.child(userId).observe(.value, with: {
// snapshot in
// let count = Int(snapshot.childrenCount)
// completion(count)
// })
db.collection("following").document(userId).getDocument { (querySnapshot, error) in
let count = Int((querySnapshot?.documentID)!)
print("followingCount::::\(String(describing: count))")
completion(count!)
}
}
}//followAPI
我试图从 firestore 获取以下计数.
I tried to get following counts from firestore.
let count = Int((querySnapshot?.documentID)!)
print("followingCount::::\(String(describing: count))")
completion(count!)
但还没有显示任何内容.我不知道我犯了什么错误?
but does not show any any yet all. I do not know what mistake i have done ?
非常感谢任何帮助....
Any help much appreciated pls....
推荐答案
如果您正在查询 collection
那么它的 snapshot
将包含一个 数组文档
.您想要获得的是一个 documentID
,它与 Firebase
中的 key
相同.
If you're querying for a collection
then its snapshot
will contain an array of documents
. What are you trying to get is a documentID
which is same as key
in Firebase
.
Firestore |Firebase
documentID = 快照.key
documentID = snapshot.key
documentData = 快照.值
documentData = snapshot.value
现在,来到重点,这里是你需要得到的计数.
Now, Come to the main point and here is what you need to get the count.
let count = querySnapshot?.documents.count
print(count)
编辑评论: 我如何迁移 REF_FOLLOWING.child(userId).observe(.value, with: { snapshot in let count = Int(snapshot.childrenCount) completion(count)) }) 到firestore
基于附加的数据库结构,您正在获取与 userId
相对应的 following
,这是一个 Document
.
Based on attached DB structure you're fetching following
corresponding to userId
which is a Document
.
REF_FOLLOWING.document(userId).getDocument { (snapshot, error) in
if let _error = error {
print(_error.localizedDescription)
return
}
guard let _snapshot = snapshot else {return}
/// This is a single document and it will give you "[String:Any]" dictionary object. So simply getting its count is the result you needed.
let dict = _snapshot.data()
print(dict.count)
}
这篇关于如何在iOS swift中使用firestore获取用户的帖子数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!