Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Android WorkManager getting reference to Worker class

Posted on November 11, 2022 By

Solution 1 :

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:

  1. First create a Book Entity in your room database.
  2. Second add a function in your Dao that returns a Flow<List>.
  3. Third collect that flow in your app (view model, repository, fragment…).
  4. Finally in your 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 😀

Problem :

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 StateFlows, like:

   override suspend fun doWork(): Result {
      //blah-blah
        withContext(Dispatchers.Default) {
            _scanResultState.collectLatest {
                //blah-blah
            }
        }
      return ...
   }

Questions:

  1. Can I somehow return/put collected StateFlow values (values are simple primitive objects) to Worker.Result?
  2. Or as an option – how could I listen/collect those StateFlow in other class – outside of my Worker?

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?

Comments

Comment posted by DarShan

1. If the values are primitive, you can put them in

Comment posted by Barmaley

How can I return a value from inside of ‘collect’? I tried but couldn’t…

READ  [FIXED] android - Is there a way to pause and resume activity during a Espresso test?
Powered by Inline Related Posts

Comment posted by Barmaley

Looks workable, but ugly 🙂 I’d prefer to have more nice solution

Comment posted by Mo Coding

That’s the only way to do it :D, and in some cases when you need to keep that data in room database it’s a good solution.

Comment posted by Barmaley

Maybe,

Comment posted by Mo Coding

Well, if you just want to share a boolean or int or any simple type, DataStore is more feasible but if you want to share an object or a list of an object you can’t do it with DataStore.

Android Tags:android, android-workmanager, java, kotlin, kotlin-coroutines

Post navigation

Previous Post: [FIXED] React-native android build fails due to missing files in the gradle cache?
Next Post: Can I use Google storage instead of iCloud?

Related Posts

[FIXED] build XML button in android studio Android
[FIXED] java – Android Firebase Show current Loged in user details Android
[FIXED] My android mobile app doesn’t working on android tv Android
[FIXED] how to define a min and max value for EditText in Android kotlin? Android
[FIXED] android studio – Dart/Flutter prevent formatter from squashing method calls to one line Android
[FIXED] java – Is it bad practice to pass a fragment as argument to RecyclerView.Adapter? To help onClick Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Quel est le fromage qui porte le nom d’une commune de Côte d’Or et qui est produit en Haute-marne ?
  • Welches ist das beste Senioren Smartphone?
  • What is ISO benefit?
  • What do red bottom shoes represent?
  • Are ultrasonic cleaners safe for electronics?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme