Update to Xcode 12: Cannot convert value of type 'DataRequest' to closure result type 'Void'

4.8k Views Asked by At

I am using AlamoFire and PromiseKit to do API calls.

The code was working fine for 2 years until I updated to Xcode 12.0.

Function now returning the error: "Cannot convert value of type 'DataRequest' to closure result type 'Void'"

My function is as follows:

   func fetchArticlesFromApi (API: String) -> Promise<[Article]> {
    return Promise<[Article]> { seal in
        return Alamofire.request(API).validate().responseString(completionHandler: { //Error happening here
            response in
            switch (response.result) {
            case .success(let responseString1):
                //Do something
            case .failure(let error):
                print (error)
                seal.reject(error)
            }
        })
    }
}

Error happening in third line of function Any thoughts what might have changed in this update?

Note: The code works fine when i run the same code on xcode 11.6!

1

There are 1 best solutions below

0
On BEST ANSWER

I found the answer for that on Github.

https://github.com/mxcl/PromiseKit/issues/1165

I shouldn't be trying to return anything from the closure passed to Promise.init. Weird how that was working in previous versions of Xcode.

To fix that I have to replace the return in front of Alamofire.request... with _ =

The function now looks like this:

    func fetchArticlesFromApi (API: String) -> Promise<[Article]> {
    return Promise<[Article]> { seal in
        _ = AF.request(API).validate().responseString(completionHandler: {
            response in
            switch (response.result) {
            case .success(let responseString1):
            //Do something
            case .failure(let error):
                print (error)
                seal.reject(error)
            }
        })
    }
}