1

I am using Alamofire to perform a network request to the dummy data source https://jsonplaceholder.typicode.com/posts and render it in my application.

I have a file called NetworkingClient.swift that abstracts most of this logic out and allows is to be reused.

public class NetworkingClient {
    typealias WebServiceResponse = ([[String: Any]]?, Error?) -> Void

    func execute(_ url: URL, completion: @escaping WebServiceResponse) {
        Alamofire.request(url).validate().responseJSON { response in
            print(response)
            if let error = response.error {
                completion(nil, error)
            } else if let jsonArray = response.result.value as? [[String: Any]] {
                completion(jsonArray, nil)
            } else if let jsonDict = response.result.value as? [String: Any] {
                completion([jsonDict], nil)
            }
        }
    }
}

I call the execute in a set up function I have in my main view controller file:

func setUpView() {
        let networkingClient = NetworkingClient()
        let posts_endpoint = "https://jsonplaceholder.typicode.com/posts"
        let posts_endpoint_url = URL(string: TEST_URL_STRING)
        networkingClient.execute(posts_endpoint_url) { (json, error) in
            if let error = error {
                print([["error": error]])
            } else if let json = json {
                print(json)
            }
        }
}

Where I call this inside viewDidLoad() under super.viewDidLoad()

I've set breakpoints inside the response in closure and I wasn't able to trigger any of them, in fact I think it's skipping the entire thing completely and I don't know why.

I am following this youtube video where the video guide does the exact same thing except their request goes through.

What am I missing?

I am using Swift 4, XCode 10, running on iOS 12.1 and my AlamoFire version is 4.7.

Q.H.
  • 1,406
  • 2
  • 17
  • 33
  • Is App Transport Security Enabled? https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9 Also `let networkingClient = NetworkingClient()` is a local instance, it might be deallocate too soon. Try with either setting a property to your ViewController class, or by using for `NetworkingClient` Class method (since it might not be important to have instances). – Larme Dec 12 '18 at 08:59
  • @Larme I originally tried having `execute` as a class method and that didn't work either. – Q.H. Dec 12 '18 at 09:01

1 Answers1

0

It's all about async stuff.your are declaring NetworkingClient object in func called setupView and Alamofire using .background thread to do stuff.so time executing of networkingClient.execute is not clear and after that setUpView deallocate from memory and all it's objects are gone including NetworkingClient.so for preventing this just declare let networkingClient = NetworkingClient() outside of function

andesta.erfan
  • 972
  • 2
  • 12
  • 30
  • Wouldn't having `execute` as a class function have the same effect? Because that didn't work either. – Q.H. Dec 12 '18 at 09:18
  • yes it has the same effect. you have to create that object outside of function.or using `Singleton` pattern to always have that object in memory – andesta.erfan Dec 12 '18 at 09:20