问题描述
我正在尝试使用Swifts AwaitKit
来实现Firebase数据库,它提供等待/异步并在后台使用Swifts PromiseKit
.问题是,当我将firebase代码放入promise时,它总是在NSURLSession
上失败,并显示此错误
I'm trying to implement firebase database with swifts AwaitKit
, which delivers await/async and uses swifts PromiseKit
under hood. The problem is that when I put firebase code inside promise it always fails on NSURLSession
with this error
但是,自称为http的自己使用https而不是http.我试图允许NSAllowsArbitraryLoads
,NSExceptionDomains
没什么帮助我.这是代码.
However http call it self uses https not http. I tried to allow NSAllowsArbitraryLoads
, NSExceptionDomains
nothings helps me. Here is code.
let data: [String: Any] = [
"test": 1,
"test2": "user"
]
let promise: Promise<Bool> = Promise { resolve, reject in
ref.child("test/picks").setValue(data) { err, _ in
if err == nil {
resolve(true)
} else {
reject(err!)
}
}
}
let isStored = try! await(promise)
推荐答案
我遇到了同样的问题,这里是使用Xcode 11 Beta 5,SwiftUI,AwaitKit,PromiseKit和Firestore的有效代码段.
I have the same issue, here a working snippet with Xcode 11 Beta 5, SwiftUI, AwaitKit, PromiseKit and Firestore.
import Foundation
import Firebase
import PromiseKit
import AwaitKit
final class UserStore: ObservableObject {
let collection: CollectionReference
init() {
collection = Firestore.firestore().collection("users")
}
func create(userId: String, familyName: String, givenName: String, email: String) {
async {
let fetchUser : Promise<String> = Promise { seal in
self.collection.document("HFlTjPHgbXH96q6LQ93N").getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
seal.fulfill(dataDescription)
} else {
seal.reject(NSError())
}
}
}
let user = try! await(fetchUser)
print("My user:", user)
}
}
}
这篇关于带有Firebase数据库的Swift AwaitKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!