在杀死/背景后,何时与context(不可能)与工人使用代码来运行代码?
我有一个简单而快速的操作,例如将重要日志插入磁盘数据库中。即使用户关闭应用程序(我们不想丢失日志),也需要保证这一点,因此我最初的预感是使用 WorkManager 来保证此任务运行并将其插入到数据库。
然而,在了解 NonCancellable 协程上下文后,我想知道 withContext(NonCancellable)
是否更好。由于向数据库添加单个事件是一个非常快的操作,我想知道在这里使用 WorkManager 是否太过分了 - API 文档说 WorkManager 应该用于长时间运行的持久工作(例如网络请求等)
在哪些情况下,在处理清理的代码之外,withContext(NonCancellable)
适合在 WorkManager
上使用?
I have a simple and fast operation such as inserting an important log into an on-disk database. This needs to be guaranteed even if the user closes the app (we don't want to lose the log), and so my initial hunch was to use WorkManager
to guarantee that this task runs and inserts it into the database.
However, upon learning about the NonCancellable coroutine context, I'm wondering if withContext(NonCancellable)
is better. Since adding a single event to a database is a very fast operation, I'm wondering if using WorkManager
is overkill here - the API documentation says WorkManager should be used for long-running, persistent work (like making network requests, etc)
What are the situations in which withContext(NonCancellable)
is appropriate to be used over WorkManager
, outside of code that handles cleanup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论