0

For the last three days I have been trying to make some progress with this, but no matter what I try I can't seem to wrap my head around how to solve this problem. This answer is what got me (I think) almost all the way there (but not quite): Capturing data from Alamofire

What I am trying to do is get the number of reviews for an app. Because Alamofire does its network calls asynchronously, I am trying to create a callback that will then allow me to actually work with the JSON that it returns. In the code below I am trying to get the reviews JSON outside of my Alamofire function and assign it to the reviewJSON variable so that I can do things with it. What am I doing wrong?

import UIKit
import SwiftyJSON
import Alamofire


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let appStoreReviewsURL: String = "https://itunes.apple.com/de/rss/customerreviews/id=529479190/json"


func getDataFromInterwebs(theURL: String, complete:(reviews: JSON) -> ()) {

    Alamofire.request(.GET, theURL).responseJSON { response in
            guard response.result.error == nil else {
                print("error calling GET ")
                print(response.result.error!)
                return
            }

            if let value = response.result.value {
                let appReviewsFromAppStore = JSON(value)
complete(reviews: appReviewsFromAppStore)
            }
            else {
                print("error parsing")
            }

    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    var reviewJson = getDataFromInterwebs(appStoreReviewsURL){ completion in
        return completion}

        print(reviewJson)
}
Community
  • 1
  • 1
Patrick
  • 31
  • 2
  • 4

1 Answers1

0

Please try following way. See if it works for you

// Change your getDataFromInterwebs like this

func getDataFromInterwebs(theURL: String, complete:(reviews: JSON) -> Void) {

                Alamofire.request(.GET, theURL).responseJSON { response in
                    guard response.result.error == nil else {
                        print("error calling GET ")
                        print(response.result.error!)
                        return
                    }

                    if let value = response.result.value {
                        let appReviewsFromAppStore = JSON(value)
                        complete(reviews: appReviewsFromAppStore)
                    }
                    else {
                        complete(reviews: "Error occured while trying to parse data")
                        print("error parsing")
                    }

                }
            }
    }

then in viewdidload is like this

    override func viewDidLoad() {
        super.viewDidLoad()
        getDataFromInterwebs(appStoreReviewsURL) { (reviews) in
            var reviewJson = reviews
            dispatch_async(dispatch_get_main_queue(), { 
                self.didGetRattingJson(reviewJson)
            })
        }
}

and finally

func didGetRattingJson(reviewJson: JSON) {
        //do whatever you want to do here
    }
Kuntal Gajjar
  • 792
  • 6
  • 12
  • Thanks Kuntal. It still doesnt seem to work though - what value does the final func didGetRattingJson take? – Patrick Jul 15 '16 at 06:25