Why does my Swift DataTask completion handler never run?

46 Views Asked by At

I am trying to write a Swift Requests struct that contains methods like get, post, put, delete, etc. to make API requests in a simplified manner.

Here is my requests.swift file:

import Foundation


struct Requests {
    
    static func makeGet(url: URL, after: @escaping (Data?, Error?) -> Void) {
        
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            print("Inside task")
            after(data, error)
        }
        task.resume()
    }
    
}

Here is my main.swift file where I call the makeGet function:

import Foundation

let url = URL(string: "https://api.edamam.com/api/food-database/v2/parser?app_id=4c38caa6&app_key=kkk&ingr=eggs 4 small".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!

Requests.makeGet(url: url) { d, e in
    print("Inside closure from main")
    print(d)
    print(e)
}

The data-task does not run and I know that because I see nothing (apart from Finished with exit code 0) output.

0

There are 0 best solutions below