swift - 如何在 swift4 中设置 firebase 事务
问题描述
在我的应用程序中,有一个按钮可以打开和关闭,但是当从多个用户单击它时,像“真假真假假假”这样的错误发生,它必须按顺序排列,但由于同时输入,像假 3 次,为此我编写了代码,但它不起作用
func stateTransaction() {
let ref = Database.database().reference().child("Button")
ref.runTransactionBlock({ (currentData: MutableData) -> TransactionResult in
ref.queryLimited(toLast: 1).observeSingleEvent(of: .childAdded) { (snapshot) in
let state = snapshot.value as! Bool
if state == false {
ref.childByAutoId().setValue(true)
ref.queryLimited(toLast: 1).observeSingleEvent(of: .childAdded, with: { (snapshot) in
print(snapshot.value as! Bool)
currentData.value = state
})
} else {
ref.childByAutoId().setValue(false)
ref.queryLimited(toLast: 1).observeSingleEvent(of: .childAdded, with: { (snapshot) in
print(snapshot.value as! Bool)
currentData.value = state
})
}
}
return TransactionResult.success(withValue: currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
}
这是我的代码,我的代码有什么问题,我该如何解决?提前感谢您。
解决方案
推荐阅读
- firebase - 当 Xamarin.Forms Android 应用程序关闭、停止或未运行时,无法显示 FCM 远程通知
- javascript - 将文件中的xml属性读入js文件
- java - 使用 Cucumber 和 Java Selenium 和 Gradle 并行运行测试
- android - Android MODIFY_PHONE_STATE 权限请求返回-1。我的应用是系统应用
- jms - Artemis ActiveMQ - AMQ159005:无效的会话模式 CLIENT_ACKNOWLEDGE
- android - 如何使用 gzip 为 React Native Android 设置获取?
- php - composer.json 中的特定提交版本会产生奇怪的要求
- php - 如何选择 php 循环中的第一个元素并向其应用一个类?
- codenameone - 代号一:使用 Fontello 和 CSS
- android - 我的应用程序中没有运行 Edittext 字段。键盘未打开