在 Swift 中使用 PromiseKit/AwaitKit 的 Firebase 调用从不 returns
Firebase calls with PromiseKit/AwaitKit in Swift never returns
类似于 Swift 中的 this unanswered question, I have been unable to use Firebase Auth and Database calls with a Promise。我可以在这样的完成处理程序中使用此类调用(只要完成处理程序不在 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:)
方法进行身份验证时会出现同样的问题;它本身工作得很好,但在承诺中永远不会 returns 。 运行 大约一分钟后,两个调用都会自动终止,并在控制台中显示以下消息:
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
}
类似于 Swift 中的 this unanswered question, I have been unable to use Firebase Auth and Database calls with a Promise。我可以在这样的完成处理程序中使用此类调用(只要完成处理程序不在 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:)
方法进行身份验证时会出现同样的问题;它本身工作得很好,但在承诺中永远不会 returns 。 运行 大约一分钟后,两个调用都会自动终止,并在控制台中显示以下消息:
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
}