flutter / flutter/flutter

[iOS] Running async dart code as part of `applicationWillTerminate`

Open
#156,139 5 comments 2 reactions 0 assignees View on GitHub
c: new feature c: proposal engine framework P2 platform-ios team-ios triaged-ios
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### Use case

According to this document: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate
We could have a function that takes up to five seconds during the `applicationWillTerminate` callback. I agree that doing complicated things there should be wrong but I think having the ability to do something is reasonable.
For exmaple, one might want to schedule a notification when a app is closed during a critical process (I believe the teala app will send a notification on close if the app is used as key).

In flutter, there is [notification_when_app_is_killed](https://pub.dev/packages/notification_when_app_is_killed) which can achieve this. However, I think its implementation is not ideal, it basically implemented eveything in swift: Overriding the `applicationWillTerminate` callback and send a notification when it is called. It is weird becuae it is not generic, also we have nice binding of notification in dart but writing swift seems defeat the point of using flutter. I think ideally, `applicationWillTerminate` should trigger a dart async function and wait for it. Then we could send notification or do other things in that function.

I found this issue: https://github.com/flutter/flutter/issues/96779, I think the idea there kinda work but does not really.
It is possible to use `FlutterMethodChannel` to call a dart function when `applicationWillTerminate` is called. However, I can't block the `applicationWillTerminate` and wait for that dart invocation to be finished. If my understanding is correct, doing so will block the main thread and `FlutterMethodChannel` requires main thread?

### Proposal

I naively think, `WidgetBindingObserver` should support this, `applicationWillTerminate` should trigger a change in the life cycle and `applicationWillTerminate` should be blocked until all `WidgetBindingObserver` callbacks are finished.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.