Skip to content

[iOS] Running async dart code as part of applicationWillTerminate #156139

@CaviarChen

Description

@CaviarChen

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 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: #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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterengineflutter/engine related. See also e: labels.frameworkflutter/packages/flutter repository. See also f: labels.platform-iosiOS applications specificallyteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions