Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 09ce0bd

Browse files
authored
[iOS] Destroy the engine prior to application termination. (#29295)
1 parent fc150a7 commit 09ce0bd

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ - (void)setupNotificationCenterObservers {
284284
name:UIApplicationWillResignActiveNotification
285285
object:nil];
286286

287+
[center addObserver:self
288+
selector:@selector(applicationWillTerminate:)
289+
name:UIApplicationWillTerminateNotification
290+
object:nil];
291+
287292
[center addObserver:self
288293
selector:@selector(applicationDidEnterBackground:)
289294
name:UIApplicationDidEnterBackgroundNotification
@@ -816,6 +821,11 @@ - (void)applicationWillResignActive:(NSNotification*)notification {
816821
[self goToApplicationLifecycle:@"AppLifecycleState.inactive"];
817822
}
818823

824+
- (void)applicationWillTerminate:(NSNotification*)notification {
825+
[self goToApplicationLifecycle:@"AppLifecycleState.detached"];
826+
[self.engine destroyContext];
827+
}
828+
819829
- (void)applicationDidEnterBackground:(NSNotification*)notification {
820830
TRACE_EVENT0("flutter", "applicationDidEnterBackground");
821831
[self surfaceUpdated:NO];

shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ - (void)handlePressEvent:(FlutterUIPressProxy*)press
124124
- (void)scrollEvent:(UIPanGestureRecognizer*)recognizer;
125125
- (void)updateViewportMetrics;
126126
- (void)onUserSettingsChanged:(NSNotification*)notification;
127+
- (void)applicationWillTerminate:(NSNotification*)notification;
128+
- (void)goToApplicationLifecycle:(nonnull NSString*)state;
127129
- (void)keyboardWillChangeFrame:(NSNotification*)notification;
128130
- (void)startKeyBoardAnimation:(NSTimeInterval)duration;
129131
- (void)ensureViewportMetricsIsCorrect;
@@ -214,6 +216,20 @@ - (void)testViewDidDisappearDoesntPauseEngineWhenNotTheViewController {
214216
OCMReject([viewControllerMock surfaceUpdated:[OCMArg any]]);
215217
}
216218

219+
- (void)testAppWillTerminateViewDidDestroyTheEngine {
220+
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
221+
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
222+
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
223+
nibName:nil
224+
bundle:nil];
225+
id viewControllerMock = OCMPartialMock(viewController);
226+
OCMStub([viewControllerMock goToApplicationLifecycle:@"AppLifecycleState.detached"]);
227+
OCMStub([mockEngine destroyContext]);
228+
[viewController applicationWillTerminate:nil];
229+
OCMVerify([viewControllerMock goToApplicationLifecycle:@"AppLifecycleState.detached"]);
230+
OCMVerify([mockEngine destroyContext]);
231+
}
232+
217233
- (void)testViewDidDisappearDoesPauseEngineWhenIsTheViewController {
218234
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
219235
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];

0 commit comments

Comments
 (0)