a2b123
a2b123

Reputation: 583

Alamofire Request Not Being Called in Closure

I am having trouble getting my Alamofire call to execute my json Closure in Swift. Below is my NetworkingService file:

import Foundation
import Alamofire
import SwiftyJSON


class NetworkingService {

    static let shared = NetworkingService()
    private init() {}

    func getSong(completionHandler: @escaping (JSON) -> Void) {
        Alamofire.request("http://10.0.1.7/myiosapp/v1/songs.php", method: .get, parameters: nil, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
            print(response)
        }
    }
}

Below is my Model class, Song:

import Foundation
import SwiftyJSON

struct Song {

    let trackName: String
    let trackArtist: String
    let trackFile: String

    // failable intializer
    init?(json: JSON) {
        guard let trackName = json["track"].string,
        let trackArtist = json["artist"].string,
        let trackFile = json["file"].string
            else { return nil }
        self.trackName = trackName
        self.trackArtist = trackArtist
        self.trackFile = trackFile

    }

}

Below is the relevant code in my HomeController file:

import UIKit
import SwiftyJSON

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)

        showJSON()

    }

    var songs = [SongTest]()

    func showJSON() {
        print("This print outs")
        NetworkingService.shared.getSong { (json) in
            print("This does not print out")
            self.songs = []
            if let listOfSongs = json["songs"].array {
                for item in listOfSongs {
                    let song = SongTest(json: item)
                    self.songs.append(song!)
                }

                for r in self.songs {
                    print(r.trackName)
                    print(r.trackArtist)
                    print(r.trackFile)
                }
            }
        }

    }
}

In my HomeController, I am able to see ("this prints out") in my console along with the JSON data from the url, which is managed in my NetworkService file. I have set a breakpoint at NetworkingService.shared.getSong { (json) in. If the JSON data is printing out, it makes me believe the error is occurring with this closure: (json). Any ideas on why this is happening and how to fix-it?

Upvotes: 1

Views: 1097

Answers (3)

a2b123
a2b123

Reputation: 583

I was able to solve my problem:

class NetworkingService {

    static let shared = NetworkingService()
    private init() {}

    func getSong(completionHandler: @escaping (JSON) -> Void) {
        Alamofire.request("http://10.0.1.7/myiosapp/v1/songs.php", method: .get, parameters: nil, encoding: URLEncoding(), headers: nil).responseJSON { (response) in

            guard let value = response.result.value else {
                completionHandler(response.result.error as! JSON)
                return
            }

            let swiftyJsonVar = JSON(value)
            completionHandler(swiftyJsonVar)

        }
    }
}

Upvotes: 1

Maxime
Maxime

Reputation: 1392

In your first method you define a completionHandler that you never call : This should works:

func getSong(completionHandler: @escaping (JSON) -> Void) {
    Alamofire.request("http://10.0.1.7/myiosapp/v1/songs.php", method: .get, parameters: nil, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
        print(response)
        completionHandler(response.result.value)
    }
}

Upvotes: 1

TheCodeTalker
TheCodeTalker

Reputation: 705

As per this code i think you missed to call the completionHandler(response)

Upvotes: 0

Related Questions