我不了解完成处理程序,我的代码中需要一个处理程序 [英] I don't understand a completion handler and I need one in my code

查看:51
本文介绍了我不了解完成处理程序,我的代码中需要一个处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Firestore捕获数据并将其分配给数组,另一个问题的人告诉我,也许我需要一个完成处理程序,但是我不知道如何工作,如果您能帮助我编写代码并进行解释,那将非常有帮助这个概念对我来说.

I capture the data from Firestore and assign to array, someone in other question told me maybe I need a completion handler but I don't understand how to work, it would be very helpful if you help me with the code and explain this concept to me.

class PetsTVC: UITableViewController {

    var db: Firestore!
    let uid = Auth.auth().currentUser?.uid
    var petslist = [String]()
    var pid = ""
    var pets = [paw]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.loadPets()
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }

    func loadPets(){
        let photo1 = UIImage(named: "Log")
        db = Firestore.firestore()

        db.collection("users").document(uid!).getDocument { documentSnapshot, error in
            guard let document = documentSnapshot else {
                return
            }
            self.petslist = document["petslist"] as? Array ?? [""]
        }
        for pet in self.petslist {
            self.db.collection("pets").document(pet).getDocument { documentSnapshot, error in
                guard let document = documentSnapshot else {
                    return
                }
                guard let data = document.data() else {
                    return
                }
                let pid = data["pid"] as! String? ?? ""
                let petName = data["petName"] as! String? ?? ""
                let infoPet = paw(id: pid, petName: petName, imagenMascota:photo1)
                self.pets.insert(infoPet, at: 0)
            }
        }
    }
}

如果您还需要代码中的其他内容,请告诉我,因为我在这里也是新手,而且我不知道要问什么.

If you need anything else from the code just tell me, because I'm also new here and I don't know what to put in the questions.

推荐答案

完成处理程序是一些需要一些时间才能完成工作的函数的代码,而您不想这样做等待它完成再继续.

A completion handler is a bit of code you hand to some function that is going to take a moment to do its job and you don't want to have to wait for it to finish before moving on.

例如,如果您的邮箱离我们很远,而您却派了一个孩子去拿邮件.他们需要花一分钟时间才能转到邮箱,获取邮件,然后再回来,所以您将继续进行其他操作.由于孩子们很容易忘记,您将它们带回后,会在纸上写下一些说明如何处理该邮件.那张纸是完成处理程序.在那张纸上最有可能的指示之一就是将部分或全部邮件交给您,但也许其中包括首先过滤掉所有垃圾邮件或其他内容.

Like if your mailbox was a ways away and you sent a kid off to get the mail. It is going to take them a minute to go to the mailbox, get the mail, and come back so you are going to carry on with other things. Since kids forget easily you write some instructions on a piece of paper for what to do with the mail once they bring it back. That piece of paper is the completion handler. Most likely one of the instructions on that piece of paper is to hand some or all of the mail to you but maybe it includes filtering out all the junk mail first or something.

在您共享的代码中,您实际上有两个完成处理程序;在开始 db.collection("users").document(uid!).getDocument 的行和开始 self.db.collection("pets").document(pet)的行中).getDocument {} 内部的所有内容都是完成处理程序(或闭包).

In the code you have shared you actually have two completion handlers; on the line that starts db.collection("users").document(uid!).getDocument and the one that starts self.db.collection("pets").document(pet).getDocument everything that is inside the { } is the completion handler (or closure).

为了将结果输入到表中,您可能需要进行两项更改.第一个更改是将} self.petslist = document ["petslist"]行之后移为?大批 ??["] ,所以该行位于 self.pets.insert(infoPet,at:0).

In order to get results into your table you likely need to make two changes. The first change is to move the } from after the line self.petslist = document["petslist"] as? Array ?? [""] so that is after the line self.pets.insert(infoPet, at: 0).

第二个更改是移动:

DispatchQueue.main.async {
    self.tableView.reloadData()
}

viewDidLoad()中移出,并放入 loadPets()中,将其放置在 self.pets.insert(infoPet,at:0)行之后>.这样,所有数据输入并处理完后,表视图将重新加载.

Out of viewDidLoad() and into loadPets() placing it after the line self.pets.insert(infoPet, at: 0). That way the table view reloads after all the data has come in and been processed.

现在,您要告诉孩子去拿邮件并将其放在篮子里,但是当孩子刚从前门出来时,试图从篮子里取出邮件,您需要等到孩子到来回来,然后从篮子里拿出邮件.

Right now you are telling your kid to go get the mail and put it in a basket but trying to get the mail out of the basket when the kid has barely made it out the front door, you need to wait until the kid gets back, then grab the mail out of the basket.

这篇关于我不了解完成处理程序,我的代码中需要一个处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆