TABResourceLoader icon indicating copy to clipboard operation
TABResourceLoader copied to clipboard

Should NetworkDataResourceService call session.invalidateAndCancel() on deinit?

Open bizz84 opened this issue 7 years ago • 1 comments

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.

bizz84 avatar Jan 30 '18 11:01 bizz84

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.

neilkimmett avatar Jan 30 '18 11:01 neilkimmett