【发布时间】:2017-03-29 23:42:13
【问题描述】:
所以我有一个函数,它有一个连接到 Firebase 数据库的内部函数。该函数返回一个布尔值,但我需要根据 Firebase 数据库中的内容返回 true 或 false。所以基本上我需要在 Firebase 函数内部返回 true 或 false 问题是当我实际上试图返回外部函数时,它认为我正在尝试返回 Firebase 函数,一个简单的例子就是这样
func someOtherFunc(name: String, lastname: String){
}
func someFunc(name: String, lastname: String) -> bool {
someOtherFunc(name: name, lastname: lastname) {
if name == "George" {
return true
} else {
return false // will not work because some other func does not return a value
}
}
}
这是我需要修复的代码,它比上面的函数稍微复杂一点,因为内部 Firebase 函数是异步运行的(在后台),所以函数内的所有代码都需要同步运行才能返回正确的值
这是我的功能
func takeAwayMoney(_ howMuch: String) -> Bool{
if let notMuch = Int(howMuch) {
let userID = FIRAuth.auth()?.currentUser?.uid
datRef.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let money = value?["money"] as? String ?? ""
//convert money to int
if let conMoney = Int(money) {
var conMoreMoney = conMoney
if conMoreMoney < notMuch {
print(" sorry you don't have enough money")
return false
} else {
conMoreMoney -= notMuch
let values = ["money": String(conMoreMoney)]
//update the users money
self.datRef.child("User").child(userID!).updateChildValues(values)
return true //doesn't work because of example above
}
}
// ...
}) { (error) in
print(error.localizedDescription)
}
}
}
此代码无法编译,因为主函数没有返回值。
我知道解决这个问题的真正困难的方法是在函数顶部初始化值,这将是用户的钱,然后在调度它几秒钟后检查它,然后你可以返回值,但是我知道肯定有其他方法,因为这种方法会导致很多问题。
【问题讨论】:
-
Firebase 是异步的,尝试返回值(即使有额外的完成句柄、线程等)确实会使事情变得过于复杂,并且需要大量额外的代码。简单的解决方案是重新考虑为什么要返回 true 或 false,以及返回的值如何影响其余代码。在这种情况下,代码会检查用户有多少钱并据此采取行动——返回的值没有被使用。更新 UI,然后仍处于关闭状态,如果有则继续下一步。这将提供流畅的用户体验。
标签: ios swift firebase firebase-realtime-database