coroutineworker | Kotlin Coroutine-based workers | Android library
kandi X-RAY | coroutineworker Summary
kandi X-RAY | coroutineworker Summary
CoroutineWorker helps support multi-threaded coroutine usage in common code that works in Kotlin/Native and on JVM until kotlinx.coroutines has full support for native, multi-threaded coroutines.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of coroutineworker
coroutineworker Key Features
coroutineworker Examples and Code Snippets
Community Discussions
Trending Discussions on coroutineworker
QUESTION
I have an operation that runs in the background with WorkManager to get data from the phone, and send it to a remote server. However, I also need to display that data in a particular activity view whenever the user happens to go to that activity. Is there a way for me to link the Worker class with the specific Activity and perform some UI changing functions? (Change text view, etc.)
...ANSWER
Answered 2022-Mar-20 at 03:55I suggest using LiveData. LiveData is an observable data class which is also lifecycle-aware. So your app components will only be updated while they are active.
The basic steps are:
- Create a ViewModel class if you don't already have one and add a
LiveData
field which contains whatever data you want to persist. (See the note in the LiveData overview for reasons why this is a good idea.) - In your
doWork
function update the LiveData object as necessary. You might want to create functions in your ViewModel to perform operations on the data. - In your activity call
observe
on the LiveData object and update the UI as necessary.
QUESTION
Using this library "androidx.work:work-runtime-ktx:2.5.0" to run a background thread. But OneTimeWorkRequest is being cancelled everything i exit the screen. What i am trying to acheive is set it run on the application level can someone help please or some hints? here is my code below :
...ANSWER
Answered 2022-Feb-11 at 12:23Fixed the issue by running the UPLOAD requests to the server
inside a GlobalScope
QUESTION
I didn't realize there was more than one constructor for periodic work requests. The clue to my confusion was in the comments of the accepted answer.
BackgroundI have a few special cases I am trying to solve for while scheduling work. One of them involves doing work immediately and then creating a periodic work request. I found this in the Android's PeriodicWorkRequest documentation:
This work executes multiple times until it is cancelled, with the first execution happening immediately or as soon as the given Constraints are met.
I figured that this meant work would execute upon creating a request. However, this was not what happened in my test implementation. (For this work there is no need for a CoroutineWorker or network connection constraints but its applicable to my business need so I am testing it)
Starting Worker
...ANSWER
Answered 2021-Sep-16 at 13:37You are overthinking it. Please dump the JS:
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging
Use adb shell dumpsys jobscheduler
And just check what are the Unsatisfied constraints in the dump:
QUESTION
My app is checking for unread emails in the background, the problem is that i need to save and retrieve lastCheckedDate when i last checked for emails so i can show only newly received emails.
For retrieving data from datastore i use observeLastCheckedDate() and i must call it with handler because if i dont i get:
java.lang.IllegalStateException: Cannot invoke observe on a background thread
Function observeLastCheckedDate() get called but while it finish(updates lastCheckedDate), workManager task is already finished with not-updated var lastchecked date.
In main class i avoid this problem by creating and invoking callback but here that does not work(it makes whole app freeze), so we somehow need to wait for that function to finish or get some new way of retreiving data from datastore.
...ANSWER
Answered 2022-Jan-03 at 18:46That's a HUGE mess with threading for simple work. (Never make your thread sleep to wait for a value)
if you going to use coroutines in the worker class. SO DON'T DO THAT
there is an alternative CoroutineWorker to extend your class from it instead of Worker
it will provide you with suspending version of doWork()
function
NOTE: remember to add the -ktx
version of the work manager dependency
QUESTION
So I replaced all of my Jobs to these Work representation.
I have two problem in the current state of refactoring.
I have many worker classes, let's say these are W1,W2,W3...
My problem is, when I schedule a work as onTime and periodic as well. I use uniq works with uniq tags. My problem is, when I queue W1 as a periodic with "W1_TAG" tag and after that I would like to create a onTime from W1 with the same tag, it doesn't starts, because there is an another Work with the same tag.
I would like to achive that, If I start W1 worker, and after that start W2 worker as periodic, and after that I start W2 as onTime, the operation will be the following:
-W1 starts -W2 periodic sees that, W1 runs, so he will wait untill w1 succeeded -w2 onTime sees that, there is an another W2 (doesn't matter that it is periodic), and he will be removed? by WorkManager, so w2 periodic remains and w2 on time wont run
I know there is an enqueUniqWork possibility, but It only append, or keep the pervious worker, and this is not working different tags.
So currently I solved this, by add different tags, but I wondering, can I samehow resolve this?
Work scheduling
...ANSWER
Answered 2021-Nov-21 at 11:33WorkManager's TAG are properties of your WorkRequest, that you set using addTag()
in the WorkRequest.Builder
:
QUESTION
I'm trying to inject WorkManager with Hilt. first I implement the documentation:
...Inject a Worker using the @HiltWorker annotation in the class and @AssistedInject in the Worker object's constructor. You can use only @Singleton or unscoped bindings in Worker objects. You must also annotate the Context and WorkerParameters dependencies with @Assisted:
ANSWER
Answered 2021-Aug-06 at 14:15The way you initialized your workmanager and workmanagerfactory is only working till workmanager version 2.5.X. With the update of Workmanager Version 2.6.x-alphaX this changed and now workmanager is using androidx.startup to initialize WorkManager.
You have two options here: Either downgrade back to 2.5.0 which I would suggest, because this is the current stable version OR change the way you initialize your workmanager.
If you want to keep your version, then change your Android Manifest the following:
QUESTION
I'm learning android development from Android_developer. while using CoroutinesWorker, I encountered a problem whilst working with Kotlin
Logcat
...ANSWER
Answered 2021-Nov-13 at 21:46I've had the same problem while working with WorkManager.
Pulling meaning from your post and the error message, this is very likely an import related issue. Check your Application dependencies.
Rather than:
QUESTION
I'm trying to run synchronous jobs using the WorkManager
however it seems as if the configuration passed is partially ignored.
Below is my implementation:
App:
...ANSWER
Answered 2021-Nov-08 at 10:51From Android documentation about workers: Doc
Note that CoroutineWorker.doWork() is a suspending function. Unlike Worker, this code does not run on the Executor specified in your Configuration. Instead, it defaults to Dispatchers.Default. You can customize this by providing your own CoroutineContext. In the above example, you would probably want to do this work on Dispatchers.IO, as follows:
QUESTION
I have an Worker that executed periodically. It connects to BLE device and syncs data from it. The connection is done by observers. doWork calling syncRides(). syncRides created an observeForever, and starts connections, when connection is established BleClient.runBleSync() called.
My concerns are the "observeForever" called every 15 min (minimal WorkManager time) and crerates observeForever that not removed. The thing is BleWorker does not have LifecycleOwner for creating "BleClient.connectionStatus.observe" instead of "BleClient.connectionStatus.observeForever". My question is should I be concerned of using observeForever and triggering it every 15 min. Or maybe you can suggest better option like adding and removing observer.
Also, when running without GlobalScope.launch(Dispatchers.Main) there is an error that this function cant run on background thread. So what does Dispatchers.Main mean when running in Worker?
...ANSWER
Answered 2021-Aug-09 at 15:23I assume that the app connect to another device on bluetooth any sync data. If my assumption is correct, first of all, you should offload the syncing process to a foreground service because the process would take a long time. Still you can use WorkManager for scheduling purposes. In the Foreground service, you should connect BLE and sync data. For this end, there are different options. If you need to use observable to observe connection status, you should use MutableSharedFlow rather than MutableLiveData thus you can observe changes with a lifecycle scope you created in the Service class. However, in my opinion, a better practice is that converting your connect() function into a suspendable function. For this you can use suspendCoroutine builder for the conversion. Additionally, if you call connect function from different threads simultaneously, you have to use lock to avoid multiple connection. Kotlin Coroutines has non-blocking locks for this. After converting your connect() function to a suspend one, you can implement your logic in linear fashion which is easy and doesn't require any kind of observation.
QUESTION
I'm trying to use WorkManager for a periodic to retrieve notifications count from the API, the problem is that I'm using hilt for the dependency injection therefore I can't inject my repository with "@Inject" even if i used @AndroidEnteryPoint. And how I can observe my retrieved data inside the "doWork" function.
...ANSWER
Answered 2021-Jul-30 at 16:22You need to inject your dependencies through a constructor. In order to enable injection to a Worker with Hilt you need to do the following.
First, annotate your Worker, its constructor and constructor arguments as such:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install coroutineworker
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page