Нужна помощь в синхронизации цикла в Swift 4.2 - PullRequest
0 голосов
/ 24 апреля 2019

Я новичок в Свифте. Создал цикл, который должен делать несколько запросов на получение alamofire с некоторыми значениями в массиве. Например, у меня есть массив, который содержит значения 1,2 и 3. Моя проблема заключается в выполнении этой синхронизации цикла.

Я пробовал много методов, но все еще не могу получить значения в порядке

      // dispatchGroup.enter()
       let urlPic = "http://asdf/"
               for id in self.array{
               var newUrlPic = urlPic + id

               Alamofire.request(newUrlPic, method: .get, headers: self.header)// edit
                   .responseJSON { response in
                       if response.result.isSuccess {
                           guard let responseData = response.data else {return}
                           let jsonDecoder = JSONDecoder()
                           print("in1")
                           self.picture = try! jsonDecoder.decode(Picture.self, from: responseData)
                           print("in2")
                           //self.dispatchGroup.leave()
                       } else {
                           print("Error: \(String(describing: response.result.error))")
                       }
                       print("in3")
               }
           }
       //dispatchGroup.notify(queue: .main){
         //  ("done all")
      //}
```    }

Actual resutls is:

http://asdf/1
http://asdf/2
http://asdf/3
in1
in2
in3
in1
in2
in3

what i want is:
http://asdf/1
in1
in2
in3
http://asdf/2
in1
in2
in3.. and so on
I know that alamofire req need some times to be done and that is the reason why i cannot get result like i want.
Would appreciated if someone could help me to change code.
...