Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The current code I'm having doens't seem to return anything, I can't find out what is causing the issue.

func getQuests(category: NSString, count: Int) -> NSArray {
    var quests = NSArray()

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
        .responseJSON { (request, response, json, error) in
            dispatch_async(dispatch_get_main_queue(), {
                quests = json as NSArray
            })
    }

    println(quests)  #=> ()

    return quests
}

Does anybody know how to solve the issue I'm having?

[Update]: This is the status.

Please look at the fifth and eight row. I can't get the assignment to quests work.

var quests = NSArray()

getQuests("normal", count: 30, completionHandler: {
    quests in
        self.quests = quests
    })

println(self.quests)  #=> ()

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

    Alamofire.request(.GET, apiUrlString, parameters: ["category": category, "count": count])
        .responseJSON { (request, response, json, error) in
            dispatch_async(dispatch_get_main_queue(), {
                quests = json as NSArray
                completionHandler(quests)
            })
    }
}

Thanks.

like image 630
Kaito Miyazaki Avatar asked Feb 11 '23 21:02

Kaito Miyazaki


1 Answers

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)")
}
like image 52
cnoon Avatar answered Feb 13 '23 20:02

cnoon