gpt4 book ai didi

ios - Firebase runTransactionBlock 在 swift 3 中不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 22:32:41 24 4
gpt4 key购买 nike

更新Xcode 8后,将Swift 2项目转为Swift 3,其他代码运行正常,但是runTransactionBlock不运行,发现这个错误:

"runTransactionBlock: usage detected while persistence is enabled. Please be aware that transactions will not be persisted across app restarts"

有什么问题吗?

Firebase 运行事务代码

postRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
var stars : Dictionary<String, Bool>
stars = post["likeMap"] as? [String : Bool] ?? [:]
var likeCount = post["likeCount"] as? Int ?? 0
if let _ = stars[uid] {
// Unstar the post and remove self from stars
likeCount -= 1
self._liked = false
stars.removeValue(forKey: uid)
} else {
// Star the post and add self to stars
likeCount += 1
self._liked = true
stars[uid] = true
}
post["likeCount"] = likeCount as AnyObject?
post["likeMap"] = stars as AnyObject?
self._likeCount = likeCount
// Set value and report transaction success
currentData.value = post

return FIRTransactionResult.success(withValue: currentData)
}
return FIRTransactionResult.success(withValue: currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}

最佳答案

不确定您使用的是哪个版本的 Firebase SDK,但我会为您提供一个我成功使用 runTransactionBlock 的基本示例:

数据库

enter image description here


代码

let ref = FIRDatabase.database().reference()
let refReservations = ref.child("reservations")


refReservations.runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in
if var data = currentData.value as? [String: Any] {
var count = data["count"] as? Int ?? 0
count += 1
data["count"] = count

currentData.value = data
}

return FIRTransactionResult.success(withValue: currentData)
}

注意事项

我使用的是 Firebase SDK 版本:3.0.1,您可以使用以下方法从您的代码中获取此信息:FIRDatabase.sdkVersion()

关于ios - Firebase runTransactionBlock 在 swift 3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39763038/

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