Alamofire completeHandler JSON response not called

I have the following code to retrieve responses in a comment list. (There are many answers in 1 comment)

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)
    }
}

A full request handler is API.listReplynever called. dispatch_group_enter(group)called once, and dispatch_group_leave (group) is never called. The code is stuck in dispatch_group_wait. Which is strange, even the user interface is stuck, which is strange, because the whole function isync.

+4
source share
2 answers

I had a similar problem:

in the main user interface thread, the call:

dispatch_semaphore_wait(loginDoneSemaphore, DISPATCH_TIME_FOREVER)

and call http using Alamofire, also use your httpRequest.responseJSON

- > - responseJSON

- > , DISPATCH_TIME_FOREVER

- > , : Alamofire responseJSON, , /,

- > Alamofire, , DISPATCH_TIME_FOREVER

- > AlamofireJSON, ,

- > : 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]
})

.

- > , : AlamofireJSON, .

+1

Alamofire, , . dispatch_group_wait dispatch_group_leave s.

, dispatch_group_notify dispatch_group_wait, , , , .

0

Source: https://habr.com/ru/post/1623521/


All Articles