I won't be able to return a value with Alamofire in Swift

跟風遠走 提交于 2019-11-28 13:51:49

The other answers are certainly correct and hit on many of the issues you are running into with asynchronous operations. I'd merely like to add the fact that the dispatch_async(dispatch_get_main_queue()) call is not necessary.

This is already being done automatically inside of Alamofire. Alamofire handles all operations on an internal delegate dispatch queue. Once all those operations are completed (validation, response serialization, etc.), your completion handler closure is called on the main dispatch queue by default. This makes the dispatch_async unnecessary and it should be removed.

You can also run your completion handlers on your own provided dispatch queue if you like, but that's certainly an advanced feature that is not applicable to this use case.

Here's a more concise version of the same logic.

let apiUrlString = "some/url/path"

func getQuests(#category: NSString, count: Int, completionHandler: (NSArray) -> Void) {
    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
             .responseJSON { _, _, json, _ in
                 completionHandler(json as NSArray)
             }
}

var myQuests: NSArray?

getQuests(category: "normal", count: 30) { quests in
    myQuests = quests
    println("My Quests: \(myQuests)")
}

When doing asynchronous work inside a function, it is not possible to return the value as you would like to. Functions that have asynchronous parts in it usually let you pass in a "completion handler", which will get executed once the asynchronous task is complete.

In your case, this would mean, you have to change your function "getQuests" like this for example:

func getQuests(category: NSString, count: Int, completionHandler: (NSArray -> Void)) {

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
        .responseJSON { (request, response, json, error) in
            dispatch_async(dispatch_get_main_queue(), {
                let quests = json as? NSArray
                // pass the array of quests, or an empty array to your completionHandler
                completionHandler(quests ?? [])
            })
    }
}

You can then call this function from somewhere and pass in the completion handler where you do something with the quests retrieved:

getQuests("Easy", count: 5, completionHandler: {
    quests in
        println(quests)
    })

Hope this gets you started.

You need to make quests a property on your class, so that you can access it from within the async callback.

var quests = NSArray()  

You're aren't going to be able to return the array, because it is async. Just remove the return type. When the callback fires, save the results to your array, and do whatever else you want to happen.

func getQuests(category: NSString, count: Int) {

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
            .responseJSON { (request, response, json, error) in
                dispatch_async(dispatch_get_main_queue(), {
                    self.quests = json as NSArray
                    println(self.quests)  #=> ()
                })
        }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!