在我的应用程序中,有一个按钮可以打开和关闭,但是当多个用户单击它时,会发生错误事件,例如“ true false true false false false false” 它必须是有序的,但是由于同时输入,所以要假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)
}
}
}
这是我的代码,我的代码有什么问题,如何解决? 谢谢你。