【发布时间】:2018-03-25 19:41:16
【问题描述】:
我无法理解如何正确使用完成处理程序,尤其是与 firebase 代码相关的情况。据我了解,完成处理程序在 Fire Base 异步完成时执行,对。因此,为了了解它的工作原理,我只想在使用 FB 收集数据时使用完成处理程序打印“DONE”。
我试过下面的代码,但是我显然不明白,因为它不起作用。
//PUTTING THE FB CODE IN FUNCTION
func test(completion: @escaping (String) -> Void) {
databaseRef.child(dataRecieverStringRecipeView!).observeSingleEvent(of: .value, with: {(snapshot) in
for item in snapshot.children.allObjects as! [DataSnapshot] {
let thisItem = item.value as! NSDictionary
let tempRecipe = Recipe()
tempRecipe.fbKey = item.key
tempRecipe.recipeHeaderObject = (thisItem["recipeHeaderFirebase"] as! String)
tempRecipe.recipeTextObject = (thisItem["recipeIngredientsTextFirebase"] as! String)
tempRecipe.recipeImageObject = (thisItem["recipeImageFirebase"] as! String)
self.recipeClassArray.append(tempRecipe) //ADD OBJECT TO ARRAY
}
}) //END LOOP
completion("DONE")
}//COMPLETION
test()//???Not sure what to do here...
【问题讨论】:
标签: swift firebase completionhandler