Testing on the simulator weirdly works for both …
Just FYI, the reason it works on the simulator is that the simulator does not enforce sandbox restrictions, and it’s the sandbox that’s prevent NSURLConnection from working on the device.
so it's possible to test both and compare speed.
Right, but that’s not a realistic comparison because the request ends up being done directly by your process rather than being bounced to a secondary process (and possibly the iPhone) as it would on a real device.
Also, contentsOf is synchrone (I mean, it returns something on the same execution loop), while NSURLSession is async and you'll have to wait.
Right, but that’s the right option anyway. There’s two possibilities here:
You’re doing the
Data(contentsOf:)
on the main thread, and that’s not going to end well. QA1693 Synchronous Networking On The Main Thread explains this.You’re doing the
Data(contentsOf:)
on a secondary thread, which is just wasting memory (the thread consumes memory to no useful effect, it’s just blocked waiting for the network). This is particularly problematic on watchOS, where the hardware doesn’t have a lot of headroom.
One question thought, is it normal that updated functions like Data/String's "contentsOf:url" are still using a deprecated method like NSURLConnection internally?
I’m not sure what you mean by “normal”. Clearly this is suboptimal, but that’s not quite the same thing (-:
If I had my druthers I’d change these
contentsOf
methods to fail if given a network URL. Support for network URLs made some degree of sense when it was originally introduced during the earlier days of Mac OS X, but it makes very little sense on our current platforms.
And if this latter isn't available on watchOS, isn't it more effective to mark it as unavailable for watchOS in the fundations?
Alas, we can’t mark it as unavailable because it is available and recommended for accessing file system URLs.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"