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 Build a Weather App with Swift (Retired) Concurrency Writing Concurrent Networking Code

Concurrency and Forecast API Request Connection Lost

I have this code (hidden apiKey is the actual value, just don't want to post it):

    private let apiKey = "hiddenValue"

    override func viewDidLoad() {
        super.viewDidLoad()
        let latLong = "40.375399,-111.964950"
        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let requestedURLToForecast = NSURL(string: latLong, relativeToURL: baseURL)
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(requestedURLToForecast, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
            println(response, error)
        })
        downloadTask.resume()
    }

The (response,error) I am getting is:

(nil, Error Domain=NSURLErrorDomain Code=-1005 "The operation couldn’t be completed. (NSURLErrorDomain error -1005.)" UserInfo=0x7ff5a15988a0 {NSErrorFailingURLStringKey=https://api.forecast.io/forecast/hiddenValue/40.375399,-111.964950, NSErrorFailingURLKey=https://api.forecast.io/forecast/hiddenValue/40.375399,-111.964950, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSUnderlyingError=0x7ff5a15280f0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1005.)"})

I looked up this error code in the docs:

kCFURLErrorNetworkConnectionLost = -1005,

So it looks like is it dropping my connection and not getting a response back. However, when I take the same URL that the error message said was requested and drop it in the browser (this is a GET request so that works fine) it responds back with JSON as expected. Not sure what is occurring that makes it lose the connection.

Thanks in advance for any help.

4 Answers

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Hey Matthew Duff and Chris Ellinger,

I poked around with the error you guys are getting and it seems like it's an Xcode issue. Are you guys using any of the Xcode 6 betas? Seems like it might be a bug. Here's a link to a discussion of the error along with some suggestions on how to fix it. Seems like the good ol' fix of restarting Xcode and the simulator works for some people.

Chris Ellinger
Chris Ellinger
26,494 Points

Yes I am using Xcode 6 beta. Thanks a bunch for researching this issue for us. Almost finished with the Weather App course and couldn't be more excited to explore everything I've learned.

Yes Pasan, I am using Xcode 6 but I am using the officially released version (I believe)(Version 6.0.1 (6A317)). Thanks for the heads up. I appreciate it.

I'm getting the same error, did anyone figure this out yet? I tried restarting Xcode, I even blew on the cartridge.

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")

        let forecastURL = NSURL(string: "40.724245,-73.562374", relativeToURL: baseURL)

        let sharedSession = NSURLSession.sharedSession()

        let downloadTask:NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler:
            {(
                location:NSURL!,
                response:NSURLResponse!,
                error: NSError!)
                -> Void in println(response,error)
            })

downloadTask.resume()

println(response,error)


(nil, Error Domain=NSURLErrorDomain Code=-1005 "The operation couldn’t be completed. (NSURLErrorDomain error -1005.)" 
UserInfo=0x7f98bb496ba0 
{NSErrorFailingURLStringKey=https://api.forecast.io/forecast/apikey/40.724245,-73.562374, 
NSErrorFailingURLKey=https://api.forecast.io/forecast/apikey/40.724245,-73.562374,
  _kCFStreamErrorDomainKey=1, 
  _kCFStreamErrorCodeKey=57, 
NSUnderlyingError=0x7f98bb496320 "The operation couldn’t be completed. 
(kCFErrorDomainCFNetwork error -1005.)"})

Same error as Matt. I waited, no luck. At this point, I'm at a loss. Since my value is nil, I can't access JSON data to workaround.

Version 6.1 (6A1052d) Yosemite

Chris Ellinger
Chris Ellinger
26,494 Points

I'm not seeing anything wrong. Of course I've been staring at a similar issue for the past 20 minutes. When just using println(response) I was getting nil in the debugger. As soon as I saw your code I changed it to println(response, error) and I got the correct response in the debugger. Is this common?

Sorry I couldn't solve your issue.

Chris, that would likely not be expected. I actually ended up taking the code from the project files and putting them in my project and I get the same error.

Then I just tried it again (after a few hours of not doing anything) and it worked fine. Maybe it was a problem with forecast's API, but that seems unlikely as me dropping it in the browser worked every time.

Chris Ellinger
Chris Ellinger
26,494 Points

I did the same thing. My code was exactly the same (except the apiKey and Lat & Long), but I copied and pasted anyway. Glad I'm not the only person having issues. I knew the code was right.

I had the same issue in Xcode 6.0.1. In my case, logging out of my company VPN (Virtual Private Network) so I could get a direct network connection to the API fixed it.

Akhil Mohanlal
Akhil Mohanlal
2,128 Points

I had the same issue... connection lost. As with Sue, I disconnected from VPN and it worked for me. Try connecting to a different n/w if you guys are facing the issue.