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

前端 未结 3 502
别跟我提以往
别跟我提以往 2020-12-11 21:48

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


        
相关标签:
3条回答
  • 2020-12-11 22:31

    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)")
    }
    
    0 讨论(0)
  • 2020-12-11 22:50

    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.

    0 讨论(0)
  • 2020-12-11 22:52

    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)  #=> ()
                    })
            }
    }
    
    0 讨论(0)
提交回复
热议问题