Handling reject with PromiseKit with Swift standard networking? - json

I've got small function that uses native solutions to parse and decode JSON. Everything works fine, except handling errors. I know that when error occurs, I should get JSON with error and message
func fetchCardDetails(withNumber number: Int) -> Promise<CardDetails> {
guard let URL = URL(string: "\(URLProvider.url)/\(APIKeyProvider.apiKey)/\(number)/") else {
fatalError("Could not reformat string to URL!")
}
var request = URLRequest(url: URL)
request.httpMethod = "GET"
let decoder = JSONDecoder()
let session = URLSession.shared
return Promise { fullfill, reject in
let dataTask = session.dataTask(with: request) { data, response, error in
if let data = data,
let json = (try? decoder.decode(CardDetails.self, from: data)) {
fullfill(json)
} else if let error = error {
reject(error)
} else if let response = response {
print(response)
} else if data != nil {
let data = NSError()
reject(data)
} else {
reject(PMKError.invalidCallingConvention)
}
}
dataTask.resume()
}
}
func fetchCardDetails(number: Int) {
_ = cardDetailsService.fetchCardDetails(withNumber: number).then { cardDetails -> Void in
//some actions with cardDetails
}
}
When getting error - nothing crashes but I am getting error Pending Promise deallocated! This is usually a bug. F.A.Q. from PromiseKit didn't help me though.
Should I maybe decode JSON with error and change whole reject closure?

This part of your if statement neither fulfills or rejects.
else if let response = response {
print(response)
}
If you neither fulfill or reject then the promise never resolves and if an unresolved promise is deallocated it prints the warning you are seeing to the console.
Please see the PromiseKit troubleshooting guide for more information.

Related

Can't access decoded JSON data (from API) outside the URLSession data task Function Scope

I am facing an issue when trying to retrieve and decode JSON data from an API. I am able to get the data from the API and decode it successfully, but I can't access it from outside the function scope. I am using the following function to get and decode data. The API returns an array of JSON objects.
func getJSON(completed: #escaping () -> ()) {
var jsonData = [API_data] () // A struct for retrieved data
let url = "URL Here" // I have the original URL here, which I can't share
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { [self]data,response,error in
guard let data = data, error == nil else{
print("error")
return
}
do{
jsonData = try JSONDecoder().decode([API_data].self,from:data)
} catch{
print(error)
}
print(jsonData[0].id) // This prints my data
})
print(jsonData[0].id) // This won't print my data
task.resume()
}
struct API_data: Codable {
let id : String
}
As a result of this, I cannot use this data anywhere in the application. Any help will be appreciated. I have also tried making jsonData a global variable, updating it in the function and returning it and then using it, still doesn't work.
Thanks for your help.
As you already have a completion handler use it and pass the received data
func getJSON(completed: #escaping ([API_data]) -> Void) {
let url = "URL Here" // I have the original URL here, which I can't shar
let task = URLSession.shared.dataTask(with: URL(string: url)!) { data, _ , error in
if let error = error { print(error); return }
do {
completed(try JSONDecoder().decode([API_data].self,from:data!))
} catch{
print(error)
}
}
task.resume()
}
and use it
getJSON { apiData in
print(apiData[0].id) // This prints my data
}
Or more comfortable with the Result type
func getJSON(completed: #escaping (Result<[API_data],Error>) -> Void) {
let url = "URL Here" // I have the original URL here, which I can't shar
let task = URLSession.shared.dataTask(with: URL(string: url)!) { data, _ , error in
if let error = error { completed(.failure(error)); return }
Result { try JSONDecoder().decode([API_data].self,from: data!) }
}
task.resume()
}
getJSON { result in
switch result {
case .success(let apiData): print(apiData[0].id) // This prints my data
case .failure(let error): print(error)
}
}
It is asynchronous, you might want to access it a bit later when it’s ready, e.g. by using the completion handler.
Try creating a completion handler from your API function and send the data with the completion. And try using it there.

JSON networking request not entering URLSession.shared.dataTask

I am having problems finding out why my dataTask returns an empty result.
While going through My NetworkingManager class it appeared that it never enters the URLSession.shared.dataTask. Does anyone know why?
Her is my NetworkingManager which is being used in the ContentView of the app:
class NetworkingManager: ObservableObject {
var didChange = PassthroughSubject<NetworkingManager, Never>()
var showList = ShowResultsAPI(results: []) {
didSet {
didChange.send(self)
}
}
init() {
guard let url = URL(string: "www.json.url") else {
return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
guard let data = data else {
return }
let showList = try! JSONDecoder().decode(ShowResultsAPI.self, from: data)
DispatchQueue.main.async {
self.showList = showList
}
}.resume()
}
}
In my opinion your coding looks correct.
Keep in mind that the request is asynch. When your debugging the URLSession.shared.dataTask you will recognize that at first the debugger is skipping the dataTask. When the URLSession receives a response it will enter the URLSession.shared.dataTask again.
I would recommend to set a breakpoint in the line with your guard statement. Then debug the process again and see if the debugger enters the process.
It would also be interesting to observe the response and the error in the completion handler to see if there are errors occuring.
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
print(error)
return
}
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200,
let data = data
else {
print("ResponseProblem")
print(response)
return
}
add return value in guard : return "example"
change your code to this :
}
}.resume()
sleep(2)
}
Adding a .sleep(2) at the end of the init() helped to process the JSON.
Edited:
It needed an asynchronous task which implies having a sleep or as #vadian suggested a better suited delay() from combine.

JSON "Invalid value around character 0" though the url works flawless and the JSON is valid

I have this json error that just randomly seems to pop up.
I load json from a domain and parse it to a dictionary. If the error doesn't occur it works flawless. Here is the code:
func retrieveCoinPairData() {
guard !checkIfBaseAndTargetAreEqual() else { return }
if let base = self.currentBase, let target = self.currentTarget {
if let url = URL(string: "https://api.cryptonator.com/api/full/\(base.code)-\(target.code)") {
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
if let error = error {
print(error.localizedDescription)
}
if let response = response {
print("reponse /api/full", response)
}
do {
if let data = data {
let jsonData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
self.createCoinPairData(with: jsonData)
} else {
self.delegate?.updateInfoLabelContent(content: .noInternetConnection)
}
} catch {
self.delegate?.updateInfoLabelContent(content: .error)
print("catch error", error, data?.description as Any)
self.retrieveCoinPairData()
}
}).resume()
} else {
self.delegate?.updateInfoLabelContent(content: .error)
}
}
}
The server response is the following throwing an 403 error:
reponse /api/full <NSHTTPURLResponse: 0x608000232400> {
URL: https://api.cryptonator.com/api/full/BTC-ETH } {
status code: 403, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Tue, 05 Jun 2018 04:23:37 GMT";
"Keep-Alive" = "timeout=15";
Server = nginx;
"Transfer-Encoding" = Identit
and the URLSession catch error is the following:
catch error Error Domain=NSCocoaErrorDomain Code=3840
"Invalid value around character 0."
UserInfo={NSDebugDescription=Invalid value around character 0.}
Optional("162 bytes")
The error occurs in the try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any] line.
Now anytime this error occurs (and it is completely random), when I check the url in the browser it works perfect.
I checked the json with jsonlint.com and it is valid, there is no top level object that makes the json need fragmentation, though that option seemed to reduce the error in the past.
I know the code 403 error tells me the website blocks access and the code 3840 error tells me there is no content to be parsed. Still I wonder where and why the error occurs.
This is the site used in this example: https://api.cryptonator.com/api/full/btc-eth
Change your lines of code to do this:
if let error = error {
print(error.localizedDescription)
return
}
if let httpResponse = response as HTTPURLResponse {
if httpResponse.statusCode == 200 {
// do your json deserialization after you verify you
// got a 200 / OK from the server
do {
if let data = data {
let jsonData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
self.createCoinPairData(with: jsonData)
} else {
self.delegate?.updateInfoLabelContent(content: .noInternetConnection)
}
} catch {
self.delegate?.updateInfoLabelContent(content: .error)
print("catch error", error, data?.description as Any)
self.retrieveCoinPairData()
}
} else {
print("reponse /api/full", response)
}
}
And what you see there is that you should only attempt JSON deserialization if you know the server sent you a 200/OK response.
There is a redirection on the url which occure sometime. You can see redirecting something like ohio.something sometime you request the url.
When the redirection happen, urlsession get the error code 403.
And also invalide value around 0 occure when json serialisation get html or other code than vald json object.
Try to remove the redirection from the webservice.
Your request should be like this, this is just demo version you have to modify it accordingly as per your requirement,
if let url = URL(string: "https://api.cryptonator.com/api/full/BTC-ETH") {
var urlRequest = URLRequest(url: url)
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.setValue("application/json", forHTTPHeaderField: "accept")
urlRequest.httpMethod = "get"
URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
guard let data = data, error == nil else {
print(error ?? "")
return
}
do {
let response = try JSONDecoder().decode(ResultResponse.self, from: data)
print(response)
} catch {
print("catch error", error.localizedDescription)
}
}.resume()
} else {
//Your else code
}
Below are two Codable classes,
struct Ticker: Codable {
let base, target, price, volume: String
let change: String
let markets: [String]
}
struct ResultResponse: Codable {
let ticker: Ticker
let timestamp: Int
let success: Bool
let error: String
}
Once you call this, you will get following output,
ResultResponse(ticker: Demo.Ticker(base: "BTC", target: "ETH", price: "12.67485919", volume: "", change: "-0.04568160", markets: []), timestamp: 1528180081, success: true, error: "")

Unexpected non-void return value in void function - JSON Data from dataTask(with: URL) - (Swift 3.0)

I'm building an iOS app in Swift 3 that's supposed to communicate with a JSON Rest Api that I'm also building myself. The app will get all sorts of content from the Api, but for now all I need it to do is check the availability of the Api through a handshake function.
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
if jsonResult["response"] as! String == "Welcome, come in!" {
print("************ RESPONSE IS: ************")
print(jsonResult)
return
} else {
return
}
} catch {
print("************ JSON SERIALIZATION ERROR ************")
}
}
}
}
task.resume()
This is the dataTask I've set up and it runs just fine (When I print the jsonResult, I get the "Welcome!" message as expected. The problem is that I want my handshake function to return true or false (So that I can give an alert if the case is false.) When I try to set up a return true or false within the if-statement, I get the error: Unexpected non-void return value in void function.
My question is: How do I return the data out of the dataTask so that I can perform checks with it within my handshake function? I'm very new to Swift so all help is appreciated :)
Below is the entire class:
import Foundation
class RestApiManager: NSObject {
var apiAvailability:Bool?
func handshake() -> Bool {
let url = URL(string: "https://api.restaurapp.nl/handshake.php")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
if jsonResult["response"] as! String == "Welcome, come in!" {
print("************ RESPONSE IS: ************")
print(jsonResult)
return true
} else {
return false
}
} catch {
print("************ JSON SERIALIZATION ERROR ************")
}
}
}
}
task.resume()
}
}
Because you're using an asynchronous API, you can't return the bool from your handshake function. If you want to show an alert in the false case, you would replace the return false with something like:
DispatchQueue.main.async {
self.showAlert()
}
Technically you could make the handshake function pause until the network stuff was done, and return the bool, but that defeats the purpose of being asynchronous, and it would freeze your app's UI during the network activity, so I doubt that's what you want to do.

Swift HTTP request works on the simulator but not in a real device

i created a watchOS app that request a value from an API and show it on a label.
It is working perfectly in the simulator but when I execute it on my Apple Watch it crashes with the following error:
[ERROR] There is an unspecified error with the connection
fatal error: unexpectedly found nil while unwrapping an Optional value
The first error is generated by my code.
The code I wrote is:
func price_request() -> NSData? {
guard let url = NSURL(string: "https://api.xxxxx.com/xxx.php") else {
return nil
}
guard let data = NSData(contentsOfURL: url) else {
print("[ERROR] There is an unspecified error with the connection")
return nil
}
print("[CONNECTION] OK, data correctly downloaded")
return data
}
func json_parseData(data: NSData) -> NSDictionary? {
do {
let json: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! Dictionary<String, AnyObject>
print("[JSON] OK!")
return (json as? NSDictionary)
} catch _ {
print("[ERROR] An error has happened with parsing of json data")
return nil
}
}
I tried also to add the App Transport Security bypass also if it is not needed because of a request to an HTTPS URL but it does not works.
Can you please help me?
Thank you
Try using NSURLSession to get data...
//declare data task
var task: URLSessionDataTask?
//setup the session
let url = URL(string:"https://url.here")!
let session = URLSession(configuration: URLSessionConfiguration.default)
task = session.dataTask(with: url){ (data, res, error) -> Void in
if let e = error {
print("dataTaskWithURL fail: \(e.localizedDescription)")
return
}
if let d = data {
//do something
}
}
task!.resume()