Errata - CH 9 Using the Network Client - Setting shared DogPatchClient


Inside DogPatchClient.swift we are asked to create a singleton pattern or “shared” instance of the DogPatchClient with the following defaults

static let shared = DogPatchClient(baseURL: URL(string:"...")!, session: URLSession(), responseQueue: nil)

The issue is that running any test will now result in a crash of…

2019-10-08 14:28:22.426415-0400 DogPatch[21272:313387] -[NSURLSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x600003777790
2019-10-08 14:28:22.429989-0400 DogPatch[21272:313387] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x600003777790'
*** First throw call stack:

This will become apparent when we implement the refreshData() method and attempt to call getDogs method.


changed the static initialization variable from URLSession() to URLSession(configuration .default)

According to the Apple documentation regarding URLSession, you should never create / instantiate a session without a configuration: Apple Developer Documentation

URLSession has a singleton shared session (which has no configuration object) for basic requests. It’s not as customizable as sessions you create, but it serves as a good starting point if you have very limited requirements. You access this session by calling the shared class method. For other kinds of sessions, you instantiate a URLSession with one of three kinds of configurations:

A default session behaves much like the shared session, but allows more configuration, and allows you to obtain data incrementally with a delegate.

Ephemeral sessions are similar to shared sessions, but don’t write caches, cookies, or credentials to disk.

Background sessions let you perform uploads and downloads of content in the background while your app isn't running.

@jrg.developer Do you have any feedback about this? Thank you - much appreciated! :]

Thanks for reporting this, @liltimtim

This is indeed the correct way to fix this.

I’ll make sure we correct this in the next update to the book. :]

@jrg.developer awesome and thanks for your reply :smiley: