FlutterEngine使用不當導致App崩潰

?筆者公司的項目最近開始使用了Flutter,由于老項目維護的時間比較長了。我們采用了原生嵌入一部分Flutter頁面的混合開發方式。項目開發完成測試階段我們遇到了一個問題。就是原生頁面跳轉到FlutterViewController(flutter頁面的根控制器,作為flutter的宿主頁面)承載的頁面時候。在返回原生頁面,重復幾次會導致APP的崩潰。下圖是官網給的示例


1.png

2.png
// 1.創建引擎
// 2.引擎啟動
// 3.引擎注冊
// 4.創建FlutterViewController
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"my flutter engine"];
  // Runs the default Dart entrypoint with a default Flutter route.
  [self.flutterEngine run];
  // Used to connect plugins (only if you have plugins with iOS platform code).
  [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
FlutterViewController *flutterViewController =
        [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];

筆者項目也是按照這個文檔步驟來的,后來經過反復的查找,定位到為題所在,在初始化引擎的方法列表里,allowHeadlessExecution控制gluttervc的生命周期。設置為NO表示,當FlutterViewController銷毀后,就停掉flutterEngine。

/**
 * Initialize this FlutterEngine with a `FlutterDartProject`.
 *
 * If the FlutterDartProject is not specified, the FlutterEngine will attempt to locate
 * the project in a default location (the flutter_assets folder in the iOS application
 * bundle).
 *
 * A newly initialized engine will not run the `FlutterDartProject` until either
 * `-runWithEntrypoint:` or `-runWithEntrypoint:libraryURI:` is called.
 *
 * @param labelPrefix The label prefix used to identify threads for this instance. Should
 *   be unique across FlutterEngine instances, and is used in instrumentation to label
 *   the threads used by this FlutterEngine.
 * @param project The `FlutterDartProject` to run.
 * @param allowHeadlessExecution Whether or not to allow this instance to continue
 *   running after passing a nil `FlutterViewController` to `-setViewController:`.
 */
- (instancetype)initWithName:(NSString*)labelPrefix
                     project:(nullable FlutterDartProject*)project
      allowHeadlessExecution:(BOOL)allowHeadlessExecution;
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容