0

I have a list of ids that I want to complete a set of functions for, each id is grabbed from an array and then a group of functions is called in the loop, I have a dispatch queue in the loop, but it seems to run through the loop too fast and before it has finished the functions for an id it has changed the id by looping, thus sending the data from the functions to the wrong places.

The ids need to be the same until all the functions in the loop finish and then start the loop again with new ids.

where am I going wrong?

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)
    })
}
Tony Merritt
  • 1,177
  • 11
  • 35
  • don't use properties in your loop. Ie. add `var` in front of `idG = i.key` and `userIDNum = idG` (actually, this second variable is redundant; just use `idG` – Paulw11 Jan 14 '20 at 20:49
  • Also, if none of your `if` statements match then you will never `leave` the dispatch group – Paulw11 Jan 14 '20 at 20:53
  • Thank you for the info @Paulw11, will this fix the issue of the loop running through before it finishes the functions? – Tony Merritt Jan 15 '20 at 02:00
  • That isn't what your problem is. It is the fact that you aren't using local variables inside the loop so the iterations interfere with each other. The dispatchGroup notify ensures that the code doesn't run until all of the data has been processed. – Paulw11 Jan 15 '20 at 02:31
  • @Paulw11 ok Thank you for the clarification it makes sense now to me. – Tony Merritt Jan 15 '20 at 17:33

0 Answers0