我被正式卡住,也陷入了回调地狱。我打电话给Firebase检索FireStore中的所有文章。每个文章对象内部都有一个Image文件名,该文件名会转换为存储引用位置,该位置需要传递给函数以获取绝对URL。我会将URL存储在数据中,但是可能会更改。问题是ArticleListener函数会在没有所有数据的情况下过早返回闭包((returnArray)),我无法弄清丢失的内容。在添加self.getURL代码之前,此方法工作正常,但现在它将数组返回为空,然后完成所有工作。
[如果有人在不使用PromiseKit或GCD的情况下将方法链接在一起有一些额外的技巧,那将是不错的选择,但是欢迎所有建议以使它按原样工作和/或重构以提高效率/可读性!
带有GCD的建议解决方案和更新的示例
这是在创建文章后称呼作者初始化。我正在尝试转换dataDict词典,以便它在键[“ author”]的Author初始化期间使用。我想我已经接近了,但不能100%确定我的GCD进入/离开呼叫是否按正确的顺序进行
public func SetupArticleListener(completion: @escaping ([Article]) -> Void) {
var returnArray = [Article]()
let db = FIRdb.articles.reference()
let listener = db.addSnapshotListener() { (querySnapshot, error) in
returnArray = [] // nil this out every time
if let error = error {
print("Error in setting up snapshot listener - \(error)")
} else {
let fireStoreDispatchGrp = DispatchGroup() /// 1
querySnapshot?.documents.forEach {
var dataDict = $0.data() //mutable copy of the dictionary data
let id = $0.documentID
//NEW EXAMPLE WITH ADDITIONAL TASK HERE
if let author = $0.data()["author"] as? DocumentReference {
author.getDocument() {(authorSnapshot, error) in
fireStoreDispatchGrp.enter() //1
if let error = error {
print("Error getting Author from snapshot inside Article getDocumentFunction - leaving dispatch group and returning early")
fireStoreDispatchGrp.leave()
return
}
if let newAuthor = authorSnapshot.flatMap(Author.init) {
print("Able to build new author \(newAuthor)")
dataDict["author"] = newAuthor
dataDict["authorId"] = authorSnapshot?.documentID
print("Data Dict successfully mutated \(dataDict)")
}
fireStoreDispatchGrp.leave() //2
}
}
///END OF NEW EXAMPLE
if let imageURL = $0.data()["image"] as? String {
let reference = FIRStorage.articles.referenceForFile(filename: imageURL)
fireStoreDispatchGrp.enter() /// 2
self.getURL(reference: reference){ result in
switch result {
case .success(let url) :
dataDict["image"] = url.absoluteString
case .failure(let error):
print("Error getting URL for author: \n Error: \(error) \n forReference: \(reference) \n forArticleID: \(id)")
}
if let newArticle = Article(id: id, dictionary: dataDict) {
returnArray.append(newArticle)
}
fireStoreDispatchGrp.leave() ///3
}
}
}
//Completion block
print("Exiting dispatchGroup all data should be setup correctly")
fireStoreDispatchGrp.notify(queue: .main) { ///4
completion(returnArray)
}
}
}
updateListeners(for: listener)
}
呼叫设置代码
self.manager.SetupArticleListener() { [weak self] articles in print("🌈🌈🌈🌈🌈🌈🌈In closure function to update articles🌈🌈🌈🌈🌈🌈🌈") self?.articles = articles }
Article Listener
public func SetupArticleListener(completion: @escaping ([Article]) -> Void) { var returnArray = [Article]() let db = FIRdb.articles.reference() let listener = db.addSnapshotListener() { (querySnapshot, error) in returnArray = [] // nil this out every time if let error = error { printLog("Error retrieving documents while adding snapshotlistener, Error: \(error.localizedDescription)") } else { querySnapshot?.documents.forEach { var dataDict = $0.data() //mutable copy of the dictionary data let id = $0.documentID if let imageURL = $0.data()["image"] as? String { let reference = FIRStorage.articles.referenceForFile(filename: imageURL) self.getURL(reference: reference){ result in switch result { case .success(let url) : print("Success in getting url from reference \(url)") dataDict["image"] = url.absoluteString print("Dictionary XFORM") case .failure(let error): print("Error retrieving URL from reference \(error)") } if let newArticle = Article(id: id, dictionary: dataDict) { printLog("Success in creating Article with xformed url") returnArray.append(newArticle) } } } } print("🌈🌈🌈🌈🌈🌈🌈 sending back completion array \(returnArray)🌈🌈🌈🌈🌈🌈🌈") completion(returnArray) } } updateListeners(for: listener) }
GetURL
private func getURL(reference: StorageReference, _ result: @escaping (Result<URL, Error>) -> Void) {
reference.downloadURL() { (url, error) in
if let url = url {
result(.success(url))
} else {
if let error = error {
print("error")
result(.failure(error))
}
}
}
}
已通过建议的解决方案和其他问题更新,我被正式卡住了,并且也陷入了回调地狱。我打电话给Firebase检索FireStore中的所有文章。在每个文章对象内部...
您需要调度组,因为for循环包含多个异步调用