refactor: Fix DataTask cancel() method design issue#42
refactor: Fix DataTask cancel() method design issue#42knine79 wants to merge 1 commit intoRecouse:mainfrom
Conversation
- Add URLSession as instance variable to DataTask class - Remove urlSession parameter from cancel() method - Update all related methods to use instance variable - Improve memory management by setting urlSession to nil after cancel - Fix issue where cancel() couldn't access the correct URLSession instance This resolves the design issue where urlSession was created inside events() function but cancel() method required it as a parameter, making it impossible to properly cancel the session from external calls.
4295467 to
cba3aef
Compare
|
@Recouse CI / Build on iOS 15 is failing because the ios-runtime cache isn’t being hit. |
|
Is there any news on this? I updated from I don't really understand why this changed and why we are now forced to pass the For the moment I'm going to lock to |
|
@xxZap Maybe @Recouse is very busy. 😅 |
|
@knine79 |
|
@xxZap could you try the main branch? As previously mentioned, I decided to hide the If that doesn't work for you, please open an issue and describe your specific use case. I'll take a look and see what I can do. I'm planning to make a new release soon. I've been busy recently and haven't had time to work on this project. |
Problem
The original code had a design issue where
urlSessionwas created inside theevents()function, but thecancel()method required it as a parameter. This caused the following problems:urlSessiononly existed within the function scope, making it impossible to access the correct session when callingcancel()from outsidecancel()method was public but couldn't receive the correctURLSessioninstance from external callsSolution
_urlSessionMutex variable to theDataTaskclass to manage the session object at the instance levelurlSessionparameters fromhandleSessionError,handleSessionResponse,parseMessages, andclosemethodsnilafter cancellation to prevent memory leaksKey Changes
_urlSession: Mutex<URLSession?>instance variable toDataTaskclasscancel()method signature fromcancel(urlSession: URLSession)tocancel()urlSessionparameter from all related methodsurlSession = nilincancel()method for better memory managementBenefits
cancel()method can now accurately cancel the session that's actually in usecancel()method is more intuitive to call without parametersTesting
Breaking Changes
This change modifies the public API of the
cancel()method. Previously it required aURLSessionparameter, now it doesn't require any parameters. This is a breaking change but improves the API design significantly.Related Issues
Fixes the design issue where external calls to
cancel()couldn't properly cancel the URLSession due to scope limitations.