【问题标题】:How do I use completion handlers in Fire Base如何在 Fire Base 中使用完成处理程序
【发布时间】: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


    【解决方案1】:

    完成处理程序基本上是 WHEN 语句。

    当一个事件发生时(例如接收到一个api调用的结果),触发completion()。你也可以通过这个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)
    
     })
    

    【讨论】:

    • 它就像一个魅力!非常感谢您为我澄清这一点。我现在对完成处理程序和使用它们有了基本的了解!惊人的! :)
    【解决方案2】:

    您需要将闭包传递给您的 test() 方法,如下所示:

    test(completion: (status: String) -> Void { print(status) })

    或者像这样:

    func completion(status: String) { print(status) } test(completion: completion)

    你也可以使用尾随闭包语法,因为你的闭包是最后一个参数:

    test() { (status) in print(status) }

    【讨论】:

    • 谢谢!这也很棒!
    • 如果你这么认为,也许你可以支持我的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多