gpt4 book ai didi

swift 3 : How to retry Firebase upload on failure

转载 作者:行者123 更新时间:2023-11-30 12:11:11 25 4
gpt4 key购买 nike

我正在 Firebase 中初始化一些数据,我将用它们来跟踪用户的事件。我需要确保这些数据写入 Firebase,所以我想知道确保关键上传成功的最佳实践是什么?

static func createUserActivityCounts(uid: String) {
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + .seconds(5)) {
let databaseReference = Database.database().reference()
let userCounts: [String: Any] = ["posts": 0,
"comments": 0,
"likes": 0]
databaseReference.child("userActivity").child(uid).child("counts").setValue(userCounts) { (error, ref) -> Void in
if error != nil {
print(error!.localizedDescription)
}
}
}
}

最佳答案

我认为解决此问题的最佳方法是在尝试查询关键上传时检查该值是否存在。如果该值不存在,则对其进行初始化。这是我为处理此问题而编写的函数。

private func getUserActivityCounts() {
if let uid = Auth.auth().currentUser?.uid {
userRef.child("userActivity").child(uid).child("counts").observeSingleEvent(of: .value, with: { snap in
if !snap.exists() {
// create user counts
if let uid = Auth.auth().currentUser?.uid {
DatabaseFunctions.createUserActivityCounts(uid: uid)
}
}
if let counts = snap.value as? [String: Any] {
if let numberOfLikes = counts["likes"] as? Int, let commentCount = counts["comments"] as? Int, let postCount = counts["posts"] as? Int {
DispatchQueue.main.async {
self.headerRef.postCount.text = String(postCount)
self.headerRef.commentCount.text = String(commentCount)
self.headerRef.likeCount.text = String(numberOfLikes)
self.collectionView.reloadData()
}
self.numberOfUserPosts = postCount
self.commentCount = commentCount
self.likeCount = numberOfLikes
}
}
})
}
}

关于 swift 3 : How to retry Firebase upload on failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46038943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com