Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Network Programming with Swift 2 Implementing a Forecast Client Testing the Networking Stack

Mert Kahraman
Mert Kahraman
12,118 Points

Everything was fine until carrying the dispatch_async code into APIClient protocol from ViewController

Hi,

Everything on my version was perfectly fine until, about 13:25 on the video, Pasan carried the dispatch_async code into fetch func in APIClient protocol from ViewController.

Error #1 In his version it works, but on mine, the compiler raises an error on the following line:

let task =JSONTaskWithRequest(request) { json, response, error in

It asks me to add self. in front of JSONTaskWithRequest, I guess so because this is inside a closure. As soon as I do this the app won't work properly.

Error #2 The compiler also complains about the task.resume() line at the very bottom of the fetch function. It says: Unresolved identifier of task.

Here is my fetch function:

func fetch<T>(request: NSURLRequest, parse: JSON -> T?, completion: APIResult<T> -> Void) {

    dispatch_async(dispatch_get_main_queue()) {
        let task = JSONTaskWithRequest(request) { json, response, error in

            guard let json = json else { 
                if let error = error {
                    completion(.Failure(error))
                } else {
                    // TODO: Implement Error Handling
                }
                return
            }

            if let value = parse(json) { 
                completion(.Success(value)) 
            } else {
                let error = NSError(domain: TRENetworkingErrorDomain, code: UnexpectedResponseError, userInfo: nil)
                completion(.Failure(error)) 
            }
        }
    }
    task.resume()

}

I would sincerely appreciate your support.

Thanks!

1 Answer

Steven Stanton
Steven Stanton
59,998 Points

It looks like you are defining the task inside a closure and then trying to call resume on it outside.

Here is my code: (A bit different because it is Swift 3)

    func fetch<T: JsonDecodable>(request: URLRequest, 
parse: @escaping (Json)->T?, 
completion: @escaping (ApiResult<T>)->Void){


        let task = self.makeJsonTask(with: request){ json, response, error in

            DispatchQueue.main.async {

            guard let json = json else {
                if let error = error{
                    completion(.failure(error))
                }else{
                    //TODO: implement error handling
                }
                return
            }

            if let value = parse(json){
                completion(.success(value))
            }else {
                let error = NSError(domain: StantNetworkingErrorDomain, code: UnexpectedResponseError, userInfo: nil)
                completion(.failure(error))
            }

            }

        }

        task.resume()

    }