NinjaDeveloper
NinjaDeveloper

Reputation: 1712

performSegue after loop is done

I have an account object with a contacts list what I am trying to do is to make a nested network call to postAccount after that postContact. sideNotes: we don't have bulk insert endpoint because of that I'm using for loop to enter contact one by one can someone help me with my nested call to performance performSegue after the loop is done

 func showUIAlertCreate(_ numberOfNewUsers:Int) {
        let alert = UIAlertController(title: "Company Created", message: "A new Account has been created and \(numberOfNewUsers) new users have been added.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
            
            DataService.shared.PostAccount("v2", "26abd11fb", UUID().uuidString, self.txtfCompanyName.text!, self.dataSource.newContacts,self.stakeholderID, completion: {  result in
                
                switch result {
                    case .success(let account):
                        DispatchQueue.main.async{
                            for contact in self.dataSource.newContacts{
                                DataService.shared.PostContacts("v2", "26abd11fb", account.accountID, contact, completion: {result in
                                    
                                    switch result {
                                        case .success(let result):
                                            break;
                                        case .failure(let error):
                                            fatalError("message: error \(error)")
                                        break;
                                    }
                                })
                                self.performSegue(withIdentifier: "segueToUsersTab", sender: nil)
                            }
                        }
                        break;
                    case .failure(let error):
                        fatalError("message: error \(error)")
                }
            })
        }))
        self.present(alert, animated: true, completion: nil)
    }

Upvotes: 1

Views: 40

Answers (1)

Piavio
Piavio

Reputation: 84

If you want to perform your segue after you have completed all of the actions in the for loop you would have to move your perform segue line to be something more like this.

DispatchQueue.main.async{
    for contact in self.dataSource.newContacts{
        DataService.shared.PostContacts("v2", "26abd11fb", account.accountID, contact, completion: {result in
            
            switch result {
                case .success(let result):
                    break;
                case .failure(let error):
                    fatalError("message: error \(error)")
                break;
            }
        })
        // old line here 
    }
    // this will only execute AFTER FOR LOOP 
    self.performSegue(withIdentifier: "segueToUsersTab", sender: nil)
}

Upvotes: 1

Related Questions