Hi
I have an array of Strings and for each one of them I need to make an Alamofire request to extract a specific value from the outputted JSON.
let array = ["A", "B","C"]
for variableName in array {
AF.request("API ENDPOINT \(variableName)").responseJSON { response in
//Parse the JSON
//Write a value to a .txt file on disk together with the variableName: i.e (variableName;JSONValue)
}
}
No problem with that.
Thing is, when I go to write the extracted values to a .txt file in the completion handler, the order of the Array is not observed. I understand that the request is async so it all depends on how much time every single request takes, hence the unordered list. So, how could I keep the .txt file list sorted following the order of the Array?
Edit:
Yes, I could create another array with all the results inside. Then sort it (using some kind of sorting algorithm to reflect the order of the initial array), and then write it to the .txt file. But I was wondering if I somehow can skip this part.
Expected result:
let array = ["A", "B","C"]
Textfile = A;(some value from the JSON request);B;(...);C;(...)
But I get this instead:
Textfile = B;(some value from the JSON request);C;(...);A;(...)