Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

ios - Alamofire network calls not being run in background thread

It is my understanding that by default, Alamofire requests run in a background thread.

When I tried running this code:

let productsEndPoint: String = "http://api.test.com/Products?username=testuser"

        Alamofire.request(productsEndPoint, method: .get)
            .responseJSON { response in
                // check for errors
                guard response.result.error == nil else {
                    // got an error in getting the data, need to handle it
                    print("Inside error guard")
                    print(response.result.error!)
                    return
                }

                // make sure we got some JSON since that's what we expect
                guard let json = response.result.value as? [String: Any] else {
                    print("didn't get products as JSON from API")
                    print("Error: (response.result.error)")
                    return
                }

                // get and print the title
                guard let products = json["products"] as? [[String: Any]] else {
                    print("Could not get products from JSON")
                    return
                }
                print(products)

        }

The UI was unresponsive until all the items from the network call have finished printing; so I tried using GCD with Alamofire:

let queue = DispatchQueue(label: "com.test.api", qos: .background, attributes: .concurrent)

    queue.async {

        let productsEndPoint: String = "http://api.test.com/Products?username=testuser"

        Alamofire.request(productsEndPoint, method: .get)
            .responseJSON { response in
                // check for errors
                guard response.result.error == nil else {
                    // got an error in getting the data, need to handle it
                    print("Inside error guard")
                    print(response.result.error!)
                    return
                }

                // make sure we got some JSON since that's what we expect
                guard let json = response.result.value as? [String: Any] else {
                    print("didn't get products as JSON from API")
                    print("Error: (response.result.error)")
                    return
                }

                // get and print the title
                guard let products = json["products"] as? [[String: Any]] else {
                    print("Could not get products from JSON")
                    return
                }
                print(products)

        }
    }

and the UI is still unresponsive as it was before.

Am I doing anything wrong here, or does the fault lie with Alamofire?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was wrong about Alamofire running on a background thread by default. It actually runs on the main queue by default. I've opened an issue on Alamofire's Github page and it's been solved here: https://github.com/Alamofire/Alamofire/issues/1922

The correct way to solve my problem was to specify what kind of queue I want my request to be run on using the "queue" parameter on the .responseJSON method (

.responseJSON(queue: queue) { response in
...
}

)

This is the entire, corrected version of my code:

let productsEndPoint: String = "http://api.test.com/Products?username=testuser"

    let queue = DispatchQueue(label: "com.test.api", qos: .background, attributes: .concurrent)

    Alamofire.request(productsEndPoint, method: .get)
        .responseJSON(queue: queue) { response in
            // check for errors
            guard response.result.error == nil else {
                // got an error in getting the data, need to handle it
                print("Inside error guard")
                print(response.result.error!)
                return
            }

            // make sure we got some JSON since that's what we expect
            guard let json = response.result.value as? [String: Any] else {
                print("didn't get products as JSON from API")
                print("Error: (response.result.error)")
                return
            }

            // get and print the title
            guard let products = json["products"] as? [[String: Any]] else {
                print("Could not get products from JSON")
                return
            }
            print(products)
     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...