取消在main()函数中启动的CRON服务。

huangapple go评论67阅读模式
英文:

Cancel CRON service that started in main()

问题

我正在启动一个CRON服务,用于在Flutter的main()函数中从Firebase获取事件列表。

我的问题是:在应用程序关闭时应该在哪里取消CRON服务?

void main() async {
  await Firebase.initializeApp();
  final cron = Cron();
  final EventFirebaseServices _eventFirebaseServices = EventFirebaseServices();
  cron.schedule(Schedule.parse('0 * * * *'), (async {
    await _eventFirebaseServices.getEventList();
  }));
}

在main函数中是否有“Dispose”方法?

谢谢,

Pierre

英文:

I am starting a CRON service, to fetch a list of events from Firebase, in flutter main().

My question is: where to cancel the CRON service when the app closes?

void main() async {
await Firebase.initializeApp();
final cron = Cron();
final EventFirebaseServices _eventFirebaseServices = EventFirebaseServices();
cron.schedule(Schedule.parse('0 * * * *), (async {
await _eventFirebaseServices.getEventList();
});
}

Is there a "Dispose" method for main?

Thanks,

Pierre

答案1

得分: 0

如果您在主方法中实例化了cron类,您无法将其处理,因为主函数内部没有dispose方法的widget生命周期。一种解决方法是将cron类制作为全局类,或者创建该cron类的全局提供程序,通过这样做,您可以在widget内部访问cron类,并在应用程序终止时清理cron的资源。在应用程序的主要widget内部(即MaterialApp widget内部的主widget),您可以扩展WidgetBindingObserver,它允许您监听应用程序暂停、恢复或终止的情况。在这种情况下,您可以访问全局cron类或该类的提供程序,并清理资源。

class MainWidget extends WidgetsBindingObserver {
  MainWidget();
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.terminated) {
      cron.cancel(); // 在应用程序终止时取消或停止cron任务
    }
  }
}

要创建一个全局类,您可以在主方法外部实例化cron类,调用Cron cron = Cron(); 相反,为了创建提供程序,我建议您开始学习这个主题,以使您的代码具有良好的管理结构。

英文:

If you are instanciating your cron class within the main method you can't dispose it becauze there is no widget lifecycle with a dispose method inside the main function. One solution is to make a global class of the cron class or make a global provider of that cron class, by doing this you can access your cron class inside a widget and clean up the resources of the cron when the app is terminated. Inside your main widget of your application ( the main widget inside the MaterialApp widget ) you can extend the WidgetBindingObserver which allow you to listen when the apps i paused resumed or terminated. Here since you have this possibility you can access your global cron class or provider of that class and clean up the resources.

class MainWidget extends WidgetsBindingObserver {
  
  MainWidget();
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.terminated) {
      cron.cancel(); // Cancel or stop the cron tasks when the app is terminated
    }
  }
}

To make a global class you can instanciate your cron class outside of the main method by calling Cron cron = Cron(); In order instead to create a provider i suggest you to start learning this topic to give to your code a well managed structure.

答案2

得分: 0

你不需要取消它,因为如果应用程序关闭,它不会运行。实际上,即使您的应用程序在后台运行,也不能保证它会运行。这个包是为服务器应用程序设计的,而不是移动应用程序,如果您查看源代码,它只是使用了Timer

英文:

You don't need to cancel it because it will not run if the app is closed. In fact, there is no guarantee that it will run even if your app is in the background. This package was designed for server apps, not mobile apps, if you check the source code it just uses Timer.

huangapple
  • 本文由 发表于 2023年7月7日 05:53:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定