Android Workmanager获得工人课程的参考
我面临以下问题:有我的课程继承coroutineworker
。该工人的逻辑已经足够复杂,实际上dowork()
调用内部其他例程和捕获(collect
)的结果几个state> state> stateflow
s ,例如:
override suspend fun doWork(): Result {
//blah-blah
withContext(Dispatchers.Default) {
_scanResultState.collectLatest {
//blah-blah
}
}
return ...
}
问题:
- 我可以以某种方式返回/将收集的
stateflow
值(值是简单的原始对象)到worker.result.result
? - 或作为选项 - 我如何在其他类中聆听/收集
stateflow
- 在我的worker
之外?
由于,worker
由workmanager
实例化的类 - 我无法参考worker
的具体实例。例如,在服务中
我可以参考实例通过绑定服务< /a>,但如果有工人,看来这是不可能的。
有什么想法吗?
I'm facing following problem: there's my class which inherits CoroutineWorker
. Logic of this worker is complicated enough, in fact doWork()
calls bunch of other routines inside and catches (collect
) results of several StateFlow
s, like:
override suspend fun doWork(): Result {
//blah-blah
withContext(Dispatchers.Default) {
_scanResultState.collectLatest {
//blah-blah
}
}
return ...
}
Questions:
- Can I somehow return/put collected
StateFlow
values (values are simple primitive objects) toWorker.Result
? - Or as an option - how could I listen/collect those
StateFlow
in other class - outside of myWorker
?
Since, Worker
class instantiated by WorkManager
- I can't have a reference to concrete instance of Worker
. E.g. in Service
I could have reference to instance through bound Services, but in case of WorkManager looks like it's impossible.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果您想分享从工人课程到应用程序的流程,则可以使用房间数据库为此,例如,假设您想共享流程&lt; list&gt;从您的
Worker
实现您需要遵循以下步骤:book Entity
。dao
中添加一个函数,该函数返回流程&lt; list&gt;。Worker
中继续将书籍添加到您的房间数据库中。通过这4个步骤,您将能够在任何其他类中从
Worker
中收集数据。我希望我的答案清晰有用:D
Well if you want to share a flow from your Worker class to your app, you can use room database for that, for example let's say that you want to share a Flow<List> from your
Worker
to achieve that you need to follow these steps:Book Entity
in your room database.Dao
that returns a Flow<List>.Worker
keep adding books to your room database.With these 4 steps you will be able to collect data from
Worker
in any other class.I hope that my answer was clear and helpful :D