我如何在 Firebase 中使用完成处理程序
How do I use completion handlers in Fire Base
我无法理解如何正确使用完成处理程序,尤其是与 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...
完成处理程序基本上是 WHEN 语句。
当事件发生时(例如收到 api 调用的结果),触发 completion()。您也可以通过此完成传递值。
func test(completion: @escaping (_ message: String) -> Void) {
databaseRef.child(dataRecieverStringRecipeView!).observeSingleEvent(of: .value, with: {(snapshot) in
//you are inside a completion handler. this code executes WHEN you've received a snapshot object
// so you loop through and process the items
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
}
// and its done here
completion("DONE")
})
}
// Now to use it...
// You need to pass in a completion handler varaible to your test function when calling it
test(completion: { message in
// WHEN you get a callback from the completion handler,
print(message)
})
您需要向 test()
方法传递闭包,如下所示:
test(completion: (status: String) -> Void {
print(status)
})
或者像这样:
func completion(status: String) {
print(status)
}
test(completion: completion)
您也可以使用尾随闭包语法,因为您的闭包是最后一个参数:
test() { (status) in
print(status)
}
我无法理解如何正确使用完成处理程序,尤其是与 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...
完成处理程序基本上是 WHEN 语句。
当事件发生时(例如收到 api 调用的结果),触发 completion()。您也可以通过此完成传递值。
func test(completion: @escaping (_ message: String) -> Void) {
databaseRef.child(dataRecieverStringRecipeView!).observeSingleEvent(of: .value, with: {(snapshot) in
//you are inside a completion handler. this code executes WHEN you've received a snapshot object
// so you loop through and process the items
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
}
// and its done here
completion("DONE")
})
}
// Now to use it...
// You need to pass in a completion handler varaible to your test function when calling it
test(completion: { message in
// WHEN you get a callback from the completion handler,
print(message)
})
您需要向 test()
方法传递闭包,如下所示:
test(completion: (status: String) -> Void {
print(status)
})
或者像这样:
func completion(status: String) {
print(status)
}
test(completion: completion)
您也可以使用尾随闭包语法,因为您的闭包是最后一个参数:
test() { (status) in
print(status)
}