ios - Firebase 在 Swift 中使用 PromiseKit/AwaitKit 调用永远不会返回

标签 ios swift firebase asynchronous promisekit

类似于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/

相关文章:

objective-c - NSTimeInterval 格式

ios - iPad SplitViewController 内存警告导致 Master View 出现问题

ios - wait_fences : failed to receive reply: 10004003 - viewDidLoad

iphone - 为此方法自定义 UITableViewCell 的手动布局?

swift - Xcode 8 beta 6 : main. swift 无法编译

Firebase 托管 : unable to get local issuer certificate

node.js - 云存储时 `node_modules`目录如何处理?

ios - 如何将 MyResult<MyData> 转换为 MyData?

iOS 后台蓝牙事件

java - Firebase Android 机器学习套件 : Way to hide display values on QR code