Eagerly free resources on `CupertinoClient.close()` (#1191)
diff --git a/pkgs/cupertino_http/CHANGELOG.md b/pkgs/cupertino_http/CHANGELOG.md
index 1a254d5..c347978 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 91e5198..00bf1d0 100644
--- a/pkgs/cupertino_http/lib/src/cupertino_api.dart
+++ b/pkgs/cupertino_http/lib/src/cupertino_api.dart
@@ -1613,4 +1613,12 @@
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 b93dd5c..4cd0646 100644
--- a/pkgs/cupertino_http/lib/src/cupertino_client.dart
+++ b/pkgs/cupertino_http/lib/src/cupertino_client.dart
@@ -228,6 +228,7 @@
@override
void close() {
+ _urlSession?.finishTasksAndInvalidate();
_urlSession = null;
}