I try to download some JSON data from a web server with NSURLSession in a WatchOS 2.2 app. The same code that runs flawlessly on the iPhone itself, takes forever on the watch (using the simulator).
I checked out this example: https://github.com/shu223/watchOS-2-Sampler, which has a function to download and display an image via NSURLSession, and it has the same problem.
The code of this example is:
let url = NSURL(string:"https://pbs.twimg.com/profile_images/3186881240/fa714ece16d0fabccf903cec863b1949_400x400.png")!
let conf = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: conf)
task = session.dataTaskWithURL(url) { (data, res, error) -> Void in
if let e = error {
print("dataTaskWithURL fail: \(e.debugDescription)")
return
}
if let d = data {
let image = UIImage(data: d)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if self.isActive {
self.image.setImage(image)
}
})
}
}
task!.resume()
Playing around with it, I figured out that if I change the code to
let session = NSURLSession.sharedSession()
it works fine.
However, I cannot use this in my app because I need to setup delegates.
What can I do to get it working?