需要帮助在Swift 4.2中进行同步Foor循环

时间:2019-04-24 10:39:11

标签: xcode uitableview synchronization alamofire swift4.2

我很快就新来了。已经创建了循环,该循环应该很少执行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.

0 个答案:

没有答案