【问题标题】:Firebase calls with PromiseKit/AwaitKit in Swift never returns在 Swift 中使用 PromiseKit/AwaitKit 调用 Firebase 永远不会返回
【发布时间】: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


【解决方案1】:

await 调用周围添加async 解决了我的问题,如下所示:

async {
    await(getData(from: "/Path/To/My/Data"))
    // continue execution
}

【讨论】:

  • 如果你不知道为什么..这是因为await是一个阻塞调用并且在mainQueue上调用它会无限期地阻塞主线程..如果promise在@上执行987654326@ 也是如此,那么它将永远不会完成,因为 mainQueue 已经被对 await 的调用阻止了..
猜你喜欢
  • 2017-12-06
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多