TABResourceLoader
TABResourceLoader copied to clipboard
Should NetworkDataResourceService call session.invalidateAndCancel() on deinit?
Use Case
My client creates a secure session which is configured for certificate pinning.
When the app starts, I create a new session like so:
URLSession(configuration: .default, delegate: self, delegateQueue: nil)
This is then passed on to all the network services in the app.
Internally, each service creates a new NetworkDataResourceService
.
The problem is that once a network service is no longer needed, it is deallocated and with it it brings down the NetworkDataResourceService
, which calls session.invalidateAndCancel()
.
Later, when I create a new network service and try to make a request, the app crashes because I'm trying to use an invalid session.
Discussion
It appears that NetworkDataResourceService
takes full ownership of the session. Is this the right thing to do?
I could inject a single NetworkDataResourceService
everywhere in the app, and this would guarantee that the session is not invalidated.
If this is the suggested approach, I would like NetworkDataResourceService
to conform to a protocol, so I can stub it out in my tests. For the time being, I can just subclass.
I agree, invalidating the session seems like an unreasonably destructive action to take here, but I may be missing some context about why its needed.