【发布时间】:2023-03-28 16:00:01
【问题描述】:
与this unanswered question 类似,我无法在Swift 中使用带有Promise 的Firebase 身份验证和数据库调用。我可以在这样的完成处理程序中使用这样的调用(只要完成处理程序不在 Promise 中):
Database.database().reference(withPath: "/Path/To/My/Data").observeSingleEvent(of: .value, with: { (result) in
// do things with the result
})
但不是这样(我使用AwaitKit 等待承诺完成):
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:) 方法进行身份验证时会出现同样的问题;它本身工作得很好,但在承诺中永远不会返回。运行大约一分钟后,两个调用都会自动终止,并在控制台中显示以下消息:
TIC TCP 连接失败 [6:0x12190e8b0]: 3:-9802 Err(-9802)
我认为问题可能在于 AwaitKit 使用信号量来等待承诺实现,但我不完全确定。
那么,我如何才能在 Promise 中成功使用 Firebase?感谢您的帮助!
【问题讨论】:
-
你的
async应该和你的await一起去哪里? -
啊,我错过了。非常感谢!
标签: ios swift firebase asynchronous promisekit