0

Good afternoon,

I'm trying to create a simple function to retrieve some information using an external API with Alamofire in Swift 2.2, but when I want to return the value it's always empty.

I tried to follow some other QA from this website and many more from other websites but I'm not able to return the value from this function.

When I print my variable (overview) inside the Alamofire it's correct, but when I try to return it, it's always empty. What I have to add (or change) in my code to make this work?

Here is my function:

func getOverview(slug: String, clientID: String) -> String {

    let movieURL: String = "https://api.trakt.tv/movies/"+slug+"?extended=full"

    let headers = ["trakt-api-version":"2", "Content-Type": "application/json", "trakt-api-key": clientID]

    var overview: String = ""

    Alamofire.request(.GET, movieURL, headers: headers).responseJSON { response in

        if response.result.isSuccess {
            let movieInfo = JSON(data: response.data!)
            overview = self.parseMovieOverview(movieInfo)
            // THAT'S CORRECT
            print(overview)
        } else {
            print(response.result.error)
        }
    }

    // THAT'S EMPTY
    return overview
}

And that's my call:

// THAT'S ALWAYS EMPTY    
overview = getOverview(slug, clientID: clientID)

Edited with the returned value overview:

func parseMovieInfo(json: SwiftyJSON.JSON, clientID: String) -> [Movie] {

    for result in json.arrayValue {

        let slug = result["ids"]["slug"].stringValue
        let title = result["title"].stringValue
        let year = result["year"].stringValue

        // OVERVIEW
        let overview = getOverview(slug, clientID: clientID)

        // PICTURE
        let picture = getPicture(slug, clientID: clientID)

        let movie = Movie(slug: slug, title: title, year: year, overview: overview, picture: picture)

        self.movies.append(movie)
    }

    return movies
}

Much appreciated,

Regards

Jordi Gámez
  • 3,400
  • 3
  • 22
  • 35

1 Answers1

1

Take a look at the most frequently asked questions on the site. Your problem is one of timing – if you were to print both of the results inside the completion handler, and outside, you will see that the function returns well before the completion handler executes. They are asynchronous, and the function call itself is gone before the results come back

Feldur
  • 1,121
  • 9
  • 23
  • Can you show me some light @Feldur? I'm still trying to make this work using completion handler but I'm not able to reach the solution. Kind regards. – Jordi Gámez Aug 01 '16 at 05:33
  • Show me what you do with the return value. – Feldur Aug 01 '16 at 05:41
  • I have added the code in my first post. I hope you can help me @Feldur because I don't know how to proceed. I'm trying to use "dataTaskWithRequest" with a completionHandler, but I have to make 3 requests and It's tricky (one for the general information, one for the overview and another one for the picture) because they are in a different API calls. – Jordi Gámez Aug 01 '16 at 05:56
  • Overview completion handler calls getpicture, which does the append in its completion handler. – Feldur Aug 01 '16 at 09:05