类似于this unanswered question ,我无法通过 Promise 使用 Firebase Auth 和数据库调用在 swift .我可以在这样的完成处理程序中使用此类调用(只要完成处理程序不在 Promise 内):
Database.database().reference(withPath: "/Path/To/My/Data").observeSingleEvent(of: .value, with: { (result) in
// do things with the result
})
但不是这样(我正在使用 AwaitKit 等待 promise 完成):
func getData(from path: String) -> Promise<DataSnapshot> {
let ref = Database.database().reference(withPath: path)
return Promise { (fulfill, reject) in
ref.observeSingleEvent(of: .value, with: { (result) in
fulfill(result) // this is never called
})
}
}
await(getData(from: "/Path/To/My/Data")) // this never completes
当我调用 await(getData(from: "/Path/To/My/Data"))
时,调用永远不会完成,并且调用后的其余代码永远不会执行。尝试使用 Auth.auth().signInAnonymously(completion:) 方法进行身份验证时也会出现同样的问题;它本身工作得很好,但在 promise 内时永远不会返回。运行大约一分钟后,两个调用都会自动终止,并在控制台中显示以下消息:
TIC TCP Conn Failed [6:0x12190e8b0]: 3:-9802 Err(-9802)
我认为问题可能在于 AwaitKit 使用信号量来等待 promise 履行,但我不完全确定。
那么我如何才能在 promise 中成功使用 Firebase?感谢您的帮助!
最佳答案
在 await
调用周围添加 async
解决了我的问题,如下所示:
async {
await(getData(from: "/Path/To/My/Data"))
// continue execution
}
关于ios - Firebase 在 Swift 中使用 PromiseKit/AwaitKit 调用永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835869/