Ham Dong Kyun
Ham Dong Kyun

Reputation: 776

NSURLSession.dataTaskWithRequest is not working

I am trying to make a generic class that will read JSON data from a URL a execute a callback with the NSDictionary created from the JSON data. I have create a static class to do that function, but it seems that it does not work after dataTaskWithRequest.

[My Code]

import Foundation

class LoadJsonFromNetwork {
    static func LoadJsonFromNetwork(url:NSURL, completion:((NSDictionary) -> ())) {
        print("Creating request")
        let urlRequest = NSMutableURLRequest(URL: url, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)
        print("Creating session")
        let session = NSURLSession.sharedSession()
        print("Send request")
        session.dataTaskWithRequest(urlRequest) {
            (data: NSData?, response: NSURLResponse?, error: NSError?) in
            print("Checking error and nil data")
            if (error == nil && data != nil) {
                print("Request json dictionary from nsdata")
                if let result = self.NSDataToJson(data!) {
                    print("Dispatching to main queue")
                    dispatch_async(dispatch_get_main_queue()) {
                        print("Calling callback")
                        completion(result)
                    }
                }
            } else {
                print(error!.description)
            }
        }
    }

    private static func NSDataToJson(data:NSData) -> NSDictionary? {
        do {
            print("Serializing JSON")
            let json: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            print("Cretaing NSDictionary")
            let result = json as? NSDictionary
            print("Returning result")
            return result
        } catch {
            return nil
        }
    }
}

[My Result in Console]

Creating request
Creating session
Send request

Upvotes: 0

Views: 1069

Answers (1)

Sohel L.
Sohel L.

Reputation: 9540

You have forget to resume() the task. Please find the below edited code of your's!

static func LoadJsonFromNetwork(url:NSURL, completion:((NSDictionary) -> ())) {
    print("Creating request")
    let urlRequest = NSMutableURLRequest(URL: url, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 15.0)
    print("Creating session")
    let session = NSURLSession.sharedSession()
    print("Send request")
    let task = session.dataTaskWithRequest(urlRequest) {
        (data: NSData?, response: NSURLResponse?, error: NSError?) in
        print("Checking error and nil data")
        if (error == nil && data != nil) {
           print("Request json dictionary from nsdata")
           if let result = self.NSDataToJson(data!) {
               print("Dispatching to main queue")
               dispatch_async(dispatch_get_main_queue()) {
                   print("Calling callback")
                   completion(result)
               }
           }
       } else {
           print(error!.description)
       }
    }
    task.resume() //you need to call this
}

Upvotes: 2

Related Questions