gpt4 book ai didi

ios - 不调用 responseJSON 的 Alamofire completionHandler

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:57 26 4
gpt4 key购买 nike

我有以下代码来获取对评论列表的回复。 (1条评论多条回复)

static func fetchCommentsAndTheirReplies(articleId: String, failure: (()->Void)?, success: (comments: [[String: AnyObject]], replies: [[[String: AnyObject]]], userIds: Set<String>)->Void) {
var retComments = [[String: AnyObject]]()
var retReplies = [[[String: AnyObject]]]()
var retUserIds = Set<String>()

Alamofire.request(.GET, API.listComment, parameters: [API.articleId: articleId]).responseJSON {
response in
guard let comments = response.result.value as? [[String: AnyObject]] else {
failure?()
return
}
print(comments)
retComments = comments

let group = dispatch_group_create()

for (commentIndex, comment) in comments.enumerate() {
guard let id = comment["_id"] as? String else {continue}

let relevantUserIds = parseRelaventUserIdsFromEntity(comment)
for userId in relevantUserIds {
retUserIds.insert(userId)
}

retReplies.append([[String: AnyObject]]())

dispatch_group_enter(group)
Alamofire.request(.GET, API.listReply, parameters: [API.commentId: id]).responseJSON {
response in
if let replies = response.result.value as? [[String: AnyObject]] {
for (_, reply) in replies.enumerate() {

let relevantUserIds = parseRelaventUserIdsFromEntity(reply)
for userId in relevantUserIds {
retUserIds.insert(userId)
}
}
//TODO: need to capture commentIndex?
retReplies[commentIndex] = replies
}
dispatch_group_leave(group)
}


}

dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
success(comments: retComments, replies: retReplies, userIds: retUserIds)
}
}

API.listReply 请求的完整处理程序永远不会被调用。 dispatch_group_enter(group) 被调用一次,dispatch_group_leave(group) 永远不会被调用。代码卡在 dispatch_group_wait。奇怪的是,连UI都卡住了,这很奇怪,因为整个功能都是异步的。

最佳答案

我遇到过类似的问题:

在主 UI 线程上,调用:

dispatch_semaphore_wait(loginDoneSemaphore, DISPATCH_TIME_FOREVER)

并使用 Alamofire 调用 http,内部也使用您的 httpRequest.responseJSON

-> 终于在 responseJSON 完成处理程序中找到了从未调用过的代码

-> 导致在 DISPATCH_TIME_FOREVER 之后查找代码从未被调用

-> 最终找到根本原因是:Alamofire 的responseJSON,默认情况下,如果不传入线程/队列,将在Main UI 线程上运行

-> 在调用 Alamofire 之前,在主 UI 线程中使用 DISPATCH_TIME_FOREVER 锁定 UI 线程

-> 所以在 Alamofire 的 responseJSON 之后,它也在主 UI 线程上运行,永远不会调用

-> 我的解决方案是:指定 Alamofire 在另一个线程上做 http 响应:

let BackgroundThread:dispatch_queue_t = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)

func dispatchBackground_async(thingsTodo:()->()) {
dispatch_async(BackgroundThread, thingsTodo)
}

dispatchBackground_async({
httpRequest.responseJSON(queue: BackgroundThread, completionHandler: { response in
gLog.debug("request=\(response.request), response=\(response.response), statusCode=\(response.response?.statusCode), result=\(response.result)")
// [Debug] [com.apple.root.background-qos] [CrifanLibHttp.swift:21]
})

这可能对你引用有用。

->也许你可以用我的方法:为Alamofire的responseJSON设置另一个线程,来解决你的问题。

关于ios - 不调用 responseJSON 的 Alamofire completionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691029/

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