android Flutter:跳过了48帧!应用程序可能在其主线程上执行了过多的工作

jhkqcmku  于 2023-06-28  发布在  Android
关注(0)|答案(1)|浏览(141)

我正面临着上述问题,而试图在数据库中执行更新时,用户关闭应用程序。如果我单击Home按钮关闭应用程序,更新到DB工作正常,更新功能工作正常,但如果我单击物理设备上的设备返回按钮关闭应用程序,则应用程序可能在主线程上做了太多工作。
我无法调试此问题。有人能帮帮忙吗。

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
      // App is in the background or closed, make the HTTP request to update
      // view counts for the series.
      updateViewCountsInFirestore();
    }
  }

在调试时,我可以看到,这个函数(updateViewCountsInFirestore)正在被调用,但请求firestore更新数据库会导致这个问题。

wi3ka0sx

wi3ka0sx1#

主线程负责处理用户界面更新和事件。由于您的操作太长且开销太大,这可能会阻塞UI。
为了解决这个问题,你应该把你的函数移到一个单独的隔离区。一个单独的隔离有它自己的内存和cpu,它被分配在主线程之外。
要创建单独的分离菌株,请使用compute方法。

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
      _updateViewCountsInFirestore();
    }
  }

  Future<void> _updateViewCountsInFirestore() async {
    await compute(updateViewCountsInFirestore());
  }

相关问题