You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The original code had a design issue where urlSession was created inside the events() function, but the cancel() method required it as a parameter. This caused the following problems:
urlSession only existed within the function scope, making it impossible to access the correct session when calling cancel() from outside
The cancel() method was public but couldn't receive the correct URLSession instance from external calls
Solution
Store URLSession as instance variable: Added _urlSession Mutex variable to the DataTask class to manage the session object at the instance level
Refactor cancel() method: Modified to be callable without parameters and use the stored session object for cancellation
Clean up method signatures: Removed unnecessary urlSession parameters from handleSessionError, handleSessionResponse, parseMessages, and close methods
Improve memory management: Set session to nil after cancellation to prevent memory leaks
Key Changes
Added _urlSession: Mutex<URLSession?> instance variable to DataTask class
Changed cancel() method signature from cancel(urlSession: URLSession) to cancel()
Removed urlSession parameter from all related methods
Added urlSession = nil in cancel() method for better memory management
Benefits
Proper session management: The cancel() method can now accurately cancel the session that's actually in use
API consistency: The cancel() method is more intuitive to call without parameters
Memory safety: Clear lifecycle management of session objects
Thread safety: Uses Mutex to handle concurrent access issues
Testing
All existing tests pass
Compilation successful
Breaking Changes
This change modifies the public API of the cancel() method. Previously it required a URLSession parameter, 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.
- 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.
@Recouse CI / Build on iOS 15 is failing because the ios-runtime cache isn’t being hit.
In another PR last month, the cache was hit, but not this time.
I’m not sure why. Could you investigate?
I updated from 0.1.4 to 0.1.5 and now I'm part of those who have a compilation issue because of the .cancel(urlSession:) param.
I don't really understand why this changed and why we are now forced to pass the URLSession since the used urlSession is an internal property inaccessible from the outside.
It really seems like a bug to me.
@xxZap Maybe @Recouse is very busy. 😅
As @Recouse mentioned in #36 (comment), cancel(urlSession:) has been changed to private last month.
It's been a while since I submitted the PR, and I haven't tested with the latest code, so I'm not sure if this is accurate, but
I think the intention is to save the DataTask returned from dataTask() and then use dataTask.cancel().
However, that code hasn't been released yet, so you'll probably need to reference the branch (main) in spm.
For reference, I switched to a different library. 😅😅😅
@knine79
I understand your choice. In fact, for the moment I'm just locking to 0.1.4 since the 0.1.5 just does not compile and there's no way it can do it due to this new URLSession parameter.
I honestly don't want to (I can't) switch right now to a different library, I hope everything will be solved soon
As previously mentioned, I decided to hide the cancel(urlSession:) method. You can achieve the same result by cancelling the parent Task, as shown in the example in the README.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.