我目前正在尝试使用 firebase 获取特定用户的所有关注者。在我的 didSet 子句中,我调用函数 setFollowingCount() 来获取当前用户关注的用户并将其分配给文本字段:
var user: User? {
didSet {
setFollowingCount()
guard let following = self.user?.following else {return}
let attributedText = NSMutableAttributedString(string: "\(following)\n", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14)])
attributedText.append(NSAttributedString(string: "followers", attributes: [NSAttributedStringKey.foregroundColor: UIColor.lightGray, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]))
self.followingLabel.attributedText = attributedText
}
}
setFollowingCount() 函数是:
func setFollowingCount(){
var i = 0
guard let userId = self.user?.uid else { return }
Database.database().reference().child("following").child(userId).observe(.value) { (snapshot) in
self.user?.following = Int(snapshot.childrenCount)
}
}
问题是,这需要很长时间才能加载,并且当您查看用户的个人资料时,通常会卡住整个应用程序。我怎样才能加快速度或更有效地工作?
最佳答案
self.user?.following = Int(snapshot.childrenCount)
这不是一个有效的解决方案。 .childrenCount
实际上循环快照并计算所有子项的数量,这会很慢。
相反,您希望将关注者数量存储为单个值,这样可以更快地检索它。
following: {
uid: {
followingCount: 100,
follwersCount: 150
}
}
然后你可以这样查询:
Database.database().reference().child("following").child(userId).observeSingleEvent(of: .value) { (snapshot) in
if let counts = snap.value as? [String: AnyObject] }
let followingCount = counts["followingCount"] as? Int
let followersCount = counts["followersCount"] as? Int
// save these values somewhere
}
})
我还建议您增加/减少事务 block 中的关注者计数,这样计数就不会困惑。看起来像这样:
static func incrementCount(countName: String) {
if let uid = Auth.auth().currentUser?.uid {
let databaseReference = Database.database().reference()
databaseReference.child("following").child(uid).runTransactionBlock { (currentData: MutableData) -> TransactionResult in
if var data = currentData.value as? [String: Any] {
var count = data[countName] as! Int
count += 1
data[countName] = count
currentData.value = data
return TransactionResult.success(withValue: currentData)
}
return TransactionResult.success(withValue: currentData)
}
}
}
最后,
如果您要使用 .observe
您需要删除引用。在这种情况下,虽然您不寻找更新,所以您可以使用 .observeSingleEvent
关于swift - 如何使用 Firebase 和 Swift 快速获取关注者和关注者数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894951/