首页 > 解决方案 > 在移动到数组中的下一个项目之前完成项目的功能。iOS

问题描述

我有一个 id 列表,我想为其完成一组函数,每个 id 从一个数组中获取,然后在循环中调用一组函数,我在循环中有一个调度队列,但它似乎在运行通过循环太快,在它完成一个 id 的函数之前,它已经通过循环改变了 id,从而将数据从函数发送到错误的地方。

在循环中的所有函数完成之前,id 必须相同,然后使用新的 id 再次开始循环。

我哪里错了?

func sendToDBProject(completion: @escaping (_ finished: Bool) -> ()) {
    self.setDate()
    for i in artistFinal {
        dispatchGroup.enter()
        idG = i.key
         print("Tony hit artist db send the artist id is \(idG)")
        let datesForMessageTemp = artistFinal[idG] as! [String]
        datesForMessage = datesForMessageTemp.joined(separator: " - ")
        userIDNum = idG
        emailAddress.append(userIDNum)
        print("Tony the id is userIDNum \(userIDNum)")
        self.getMessagesCount(completion: { (complete) in
            print("Tony got to end message num artist")
            self.getProjects(completion: { (complete) in
                print("Tony got to end get projects num artist")
                self.sendToDBUserMessage(completion: { (complete) in
                    print("Tony got to end message snt artist")
                    self.updateBadgeNum(completion: { (complete) in
                        print("Tony got messages count")
                        print("Tony ther status is \(self.statusToSet)")
                        if self.statusToSet == "Released" {
                            self.updateUserProjDBRelease(completion: { (complete) in
                                print("Tony got to end release artist")

                                self.dispatchGroup.leave()
                            })
                        }else if self.statusToSet == "Pencil" {

                            self.updateUserProjDBPencil(completion: { (complete) in
                                print("Tony hit end 1 info artist artist db send the artist id is \(self.idG)")

                                self.dispatchGroup.leave()
                            })
                        }else if self.statusToSet == "Book" {

                            self.updateUserProjDBBook(completion: { (complete) in
                                print("Tony got to end book artist")

                                self.dispatchGroup.leave()
                            })
                        }
                    })
                })
            })
        })
    }

    self.dispatchGroup.notify(queue: DispatchQueue.main, execute: {
        print("Tony got to end all artist")
        completion(true)
    })
}

标签: iosxcodefunctionloopsdispatch-queue

解决方案


推荐阅读