From dd31e64685b03a9700cc99d7b7fc637dd60c4d3a Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 2 May 2024 15:26:57 -0700 Subject: [PATCH] Eagerly free resources on `CupertinoClient.close()` (#1191) --- pkgs/cupertino_http/CHANGELOG.md | 5 +++-- pkgs/cupertino_http/lib/src/cupertino_api.dart | 8 ++++++++ pkgs/cupertino_http/lib/src/cupertino_client.dart | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkgs/cupertino_http/CHANGELOG.md b/pkgs/cupertino_http/CHANGELOG.md index 1a254d5317..c34797872c 100644 --- a/pkgs/cupertino_http/CHANGELOG.md +++ b/pkgs/cupertino_http/CHANGELOG.md @@ -2,10 +2,11 @@ * Upgrade to `package:ffigen` 11.0.0. * Bring `WebSocket` behavior in line with the documentation by throwing - `WebSocketConnectionClosed` rather `StateError` when attempting to send + `WebSocketConnectionClosed` rather than `StateError` when attempting to send data to or close an already closed `CupertinoWebSocket`. * Update minimum supported iOS/macOS versions to be in sync with the minimum - (best effort) supported for Flutter: iOS 12, macOS 10.14 + (best effort) supported for Flutter: iOS 12, macOS 10.14. +* Eagerly free resources on `CupertinoClient.close()`. ## 1.4.0 diff --git a/pkgs/cupertino_http/lib/src/cupertino_api.dart b/pkgs/cupertino_http/lib/src/cupertino_api.dart index 91e5198e19..00bf1d0d22 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_api.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_api.dart @@ -1613,4 +1613,12 @@ class URLSession extends _ObjectHolder { onWebSocketTaskClosed: _onWebSocketTaskClosed); return task; } + + /// Free resources related to this session after the last task completes. + /// Returns immediately. + /// + /// See [NSURLSession finishTasksAndInvalidate](https://developer.apple.com/documentation/foundation/nsurlsession/1407428-finishtasksandinvalidate) + void finishTasksAndInvalidate() { + _nsObject.finishTasksAndInvalidate(); + } } diff --git a/pkgs/cupertino_http/lib/src/cupertino_client.dart b/pkgs/cupertino_http/lib/src/cupertino_client.dart index b93dd5c29e..4cd0646946 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_client.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_client.dart @@ -228,6 +228,7 @@ class CupertinoClient extends BaseClient { @override void close() { + _urlSession?.finishTasksAndInvalidate(); _urlSession = null; }