gpt4 book ai didi

ios - DispatchGroup.wait() 卡住程序

转载 作者:行者123 更新时间:2023-12-01 19:51:18 28 4
gpt4 key购买 nike

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

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() 在不同线程上被调用。我将调用移到了网络请求的开头和结尾,现在它工作正常。

关于ios - DispatchGroup.wait() 卡住程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736271/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com