0

I have a for loop that runs an asynchronous network function in each loop, and I want to make it so that the loop only moves on after the network function has been completed.

for item in array {

    theo.createNode(item.node, completionBlock: { (node, error) in
             // callback               
        })

}

As seen in my code, I only want the loop to move onto the next item after the createNode function has been completed and callback is received. I'm not looking to run something after all the async functions in the array have run; I'm looking to run the async function in each item to completion before moving onto the next one. Is there any method or listener that accomplishes this?

bigreddawg
  • 723
  • 1
  • 5
  • 10
  • See also https://stackoverflow.com/questions/48254073/calling-asynchronous-method-inside-for-loop, which has links to more similar questions. – Martin R Aug 27 '18 at 20:03
  • @MartinR I've checked out all of those questions and their answers, and sadly they do not provide the specific solution for my issue. Those questions solve for detecting when all the asynchronous functions in a for loop have completed; what I need is an approach that continues onto the next item in a for loop only after the asynchronous function in the current iteration is completed. Basically, I'm not looking to run something after all the async functions have run; I'm looking to run the async function in each item to completion before moving onto the next one. – bigreddawg Aug 28 '18 at 04:57
  • Have a look at https://stackoverflow.com/a/47735350/1187415 – Martin R Aug 28 '18 at 05:30

0 Answers0