refactor: Fix DataTask cancel() method design issue
Problem
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:
urlSessiononly existed within the function scope, making it impossible to access the correct session when callingcancel()from outside- The
cancel()method was public but couldn't receive the correctURLSessioninstance from external calls
Solution
- Store URLSession as instance variable: Added
_urlSessionMutex variable to theDataTaskclass 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
urlSessionparameters fromhandleSessionError,handleSessionResponse,parseMessages, andclosemethods - Improve memory management: Set session to
nilafter cancellation to prevent memory leaks
Key Changes
- Added
_urlSession: Mutex<URLSession?>instance variable toDataTaskclass - Changed
cancel()method signature fromcancel(urlSession: URLSession)tocancel() - Removed
urlSessionparameter from all related methods - Added
urlSession = nilincancel()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.
@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?
Is there any news on this?
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.
For the moment I'm going to lock to 0.1.4
@xxZap Maybe @Recouse is very busy. π
As @Recouse mentioned in https://github.com/Recouse/EventSource/pull/36#issuecomment-3492947985, 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. π
π
π