DispatchGroup.wait() 冻结程序

DispatchGroup.wait() freezes program

我正在进行两个异步网络调用,并且想使用调度组等待调用完成然后恢复。我的程序卡住了。

class CommentRatingViewController: UIViewController, UITextViewDelegate {
let myDispatchGroup = DispatchGroup()

@IBAction func saveRatingComment(_ sender: Any) {
        rating = ratingView.rating
        if rating != 0.0 {
            myDispatchGroup.enter()
            saveRating(articleID: post.articleID, userID: post.userID) //Network call
            self.updatedRating = true
        }
        if commentsTextView.text != "" {
            myDispatchGroup.enter()
            saveComment(articleID: post.articleID, userID: post.userID, comment: commentsTextView.text!) //Network call                self.addedComment = true
        }
        myDispatchGroup.wait()
        DispatchQueue.main.async {
            self.delegate?.didCommentOrRatePost(updatedRating: self.updatedRating, addedComment: self.addedComment)
        }
    }

这是网络调用之一:

func saveRating (articleID: String, userID: String) {

            let userPostURLRaw = "http://www.smarttapp.com/DesktopModules/DnnSharp/DnnApiEndpoint/Api.ashx?method=UpdatePostRating"
            Alamofire.request(
                userPostURLRaw,
                method: .post,
                parameters: ["articleID": articleID,
                             "newRating": self.rating,
                             "UserID": userID]
            )
                .responseString { response in
                    guard let myString = response.result.value else { return }

                DispatchQueue.main.async {
                    self.myDispatchGroup.leave()
                }
            }
    }

在我引入 Dispatch Group 代码之前,网络调用一直有效。

我已经解决了这个问题。

问题是 myDispatchGroup.enter()self.myDispatchGroup.leave() 在不同的线程上被调用。我将调用移到了网络请求的开头和结尾,现在它工作正常。