For-loop和forEach()行为异常

时间:2019-05-15 20:55:18

标签: arrays swift for-loop

我正在进行5个请求,一个接一个。第一个请求是获取配置文件ID和访问令牌。其他人正在下载每个配置文件ID的数据。

我正在做forEach(),并为我拥有的每个个人资料ID请求数据。当我重新加载数据时,奇怪的事情开始了。多数情况下是正确的,但有时它首先获取索引1的数据,然后获取0的数据。我不明白为什么。我尝试了for item in id,并尝试将配置文件ID保存到另一个数组中并使用它,但结果相同。我没有尝试过的是确切告诉它应该使用什么索引。但是我不明白for循环和请求如何协同工作。有时请求接after而至,有时却没有。

self.api.getProfileData(access_token: token) { (profiles, err)  in
    if err != nil {
        print("ERROR: \(err!)")
    } else {
        if let profile = profiles {
            let id = profile[0].data.profiles
            let token = profile[0].data.access_token
            if self.didLogin == true {
                self.profileId = id
            }
            self.group.enter()
            var i = 0
            id.enumerated().forEach({ (index, prof) in
                self.api.getProfiles(token: token!, profileId: prof.id!, completion: { (profileArray) in
                    debugPrint("PROFILES: \(prof.id!) At index: \(index)")
                    if let profileArray = profileArray {
                        self.profile = profileArray
                    }
                    self.api.getMarks(token: token!, profileId: prof.id!, completion: { (marksArray) in
                        debugPrint("MARKS: \(prof.id!) At index: \(index)")
                        if let marksArray = marksArray {
                            self.marks = marksArray
                        }
                        self.api.getBUP(token: token!, profileId: prof.id!, completion: { (bup) in
                            debugPrint("BUP: \(prof.id!) At index: \(index)")
                            if let bup = bup {
                                self.bup = bup
                            }
                            self.api.getRUP(token: token!, profileId: prof.id!, completion: { (rup) in
                                debugPrint("RUP: \(prof.id!) At index: \(index)")
                                if let rup = rup {
                                    self.rup = rup

                                    i += 1
                                    if i == profile[0].data.profiles.count {
                                        self.group.leave()
                                    }
                                }
                            })
                        })
                    })
                })
                self.group.notify(queue: .main, execute: {
                    if i != profile[0].data.profiles.count {
                        i += 1
                    } else {
                        SVProgressHUD.dismiss()
                        UIView.transition(with: self.tableView, duration: 0.2, options: .transitionCrossDissolve, animations: {
                            //                                                if self.didLogin == true {
                            self.tableView.reloadData()
                            //                                                }
                        }, completion: {(completed) in
                            if completed {
                                self.tableView.isUserInteractionEnabled = true
                                self.didLogin = false
                            }
                        })
                    }
                })
            })
        }
    }
}

我希望这样:

"PROFILES: 5176 At index: 0"
"PROFILES: 5177 At index: 1"
"MARKS: 5176 At index: 0"
"MARKS: 5177 At index: 1"
"BUP: 5176 At index: 0"
"BUP: 5177 At index: 1"
"RUP: 5176 At index: 0"
"RUP: 5177 At index: 1"

但是我得到了

"PROFILES: 5176 At index: 0"
"PROFILES: 5177 At index: 1"
"MARKS: 5177 At index: 1"
"MARKS: 5176 At index: 0"
"BUP: 5177 At index: 1"
"BUP: 5176 At index: 0"
"RUP: 5176 At index: 0"
"RUP: 5177 At index: 1"

"PROFILES: 5176 At index: 0"
"PROFILES: 5177 At index: 1"
"MARKS: 5177 At index: 1"
"MARKS: 5176 At index: 0"
"BUP: 5176 At index: 0"
"BUP: 5177 At index: 1"
"RUP: 5176 At index: 0"
"RUP: 5177 At index: 1"

1 个答案:

答案 0 :(得分:1)

致电时,

styles

它是一个id.enumerated().forEach({ (index, prof) in { ... } 呼叫。似乎id数组中有两个元素,并且从.forEach中分支出两个根调用。对于每个呼叫,将打印4条日志。配置文件5176的四个日志,配置文件5177的其他四个日志。

您应该知道asynchronous调用不能保证您执行两个线程的顺序。这意味着配置文件5176和5177的日志顺序不希望是连续的,但是单个配置文件(即5176)下的所有日志都是顺序的,因为它们是在每次完成时打印的。

请深入了解asynchronous and synchronous呼叫流程的工作原理。

相关问题