I am using Android WorkManager(version 1.0.0-alpha13) to get some data from my server and store it on local database. Used following worker, without any operations and just returned success. doWork() run repeatedly,even after task successfully completed. Is it normal behaviour of Worker or is it any issue in this version?
HomeRepository.kt
val fetchVideosWorker = OneTimeWorkRequest.Builder(FetchVideosWorker::class.java)
.build()
val enqueue = WorkManager.getInstance().enqueue(fetchVideosWorker)
FetchVideosWorker.kt
class FetchVideosWorker(context : Context, params : WorkerParameters) : Worker(context,params) {
override fun doWork(): Result {
Log.d("youtubevideo","doWork")
return Result.success()
}
}
Related
I created a class for worker, inside worker class I need to have api call and save the api response in room database. Before api call, I will check for the connectivity. For that I referred to this article
https://medium.com/#alexzaitsev/android-viewmodel-check-network-connectivity-state-7c028a017cd4
im using this connectivity in multiple places of my application, so im reusing it inside MyWorker class.
My worker looks like below:
class MyWorker(private val context: Context, parameters: WorkerParameters) :
Worker(context, parameters) {
lateinit var connectionLiveData: ConnectionLiveData
override fun doWork(): Result {
connectionLiveData = ConnectionLiveData(applicationContext)
GlobalScope.launch(Dispatchers.Main) {
connectionLiveData.observeForever { isConnected ->
isConnected?.let {
Api.enqueue(object : Callback<Response> {
override fun onFailure(call: Call<Response>, t: Throwable) {
t.printStackTrace()
}
override fun onResponse(call: Call<Response>, response: Response<Response>) {
if (response.isSuccessful) {
// save in db
}
}
}
}
}
}
}
I'm using this api call, so that user can initiate this api call and can navigate to other fragments or activity. Meanwhile, it will update the db in background without disturbing the user.
My question is, from my knowledge I know "Worker" will run in background. And "Dispatchers.Main" will run in Main thread.
If I use "Dispatchers.Main" inside "Worker", then my api call will happen in Main thread? If it runs in main thread, then my usage of worker is of no use? I'm using "Dispatchers.Main" to check the network, so it is not allowing me to use "Dispatchers.IO"(becoz observeForever), so my worker is waste?
I run the app, it works fine. I couldn't notice that it blocks the UI or main thread. I suspect it may lag in the future, when api response takes time. So can you explain which thread it will execute?
Any android studio tool is there to see in which thread all code executing?
In my app I start a WebSocketWorker tasks that runs periodically every 15 minutes. As the name implies, it contains a WebSocket for listening to a socket in the background:
// MainApplication.kt
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
val work = PeriodicWorkRequestBuilder<WebSocketWorker>(15, TimeUnit.MINUTES).build()
workManager.enqueueUniquePeriodicWork("UniqueWebSocketWorker", ExistingPeriodicWorkPolicy.KEEP, work)
}
The WebSocketWorker contains the following logic:
#HiltWorker
class WebSocketWorker #AssistedInject constructor(
#Assisted appContext: Context,
#Assisted workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
inner class MyWebSocketListener : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
Timber.d("The message sent is %s", text)
// do sth. with the message
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
t.localizedMessage?.let { Timber.e("onFailure: %s", it) }
response?.message?.let { Timber.e("onFailure: %s", it) }
}
}
override suspend fun doWork(): Result {
try{
// code to be executed
val request = Request.Builder().url("ws://***.***.**.***:8000/ws/chat/lobby/").build()
val myWebSocketListener = MyWebSocketListener()
val client = OkHttpClient()
client.newWebSocket(request, myWebSocketListener)
return Result.success()
}
catch (throwable:Throwable){
Timber.e("There is a failure")
Timber.e("throwable.localizedMessage: %s", throwable.localizedMessage)
// clean up and log
return Result.failure()
}
}
}
As you can see, in the Worker class I set the WebSocket and everything is fine. Listening to the socket works.
Now, I also want to add the "sending of messages" functionality to my app. How can I reuse the websocket created in WebSocketWorker? Can I pass input data to the WebSocketWorker that runs in the background ?
Let's say I have a EditText for typing the message and a Button to send the message with a setOnClickListener attached like this:
binding.sendButton.setOnClickListener {
// get message
val message = binding.chatMessageEditText.text.toString()
// check if not empty
if(message.isNotEmpty()) {
// HOW CAN I REUSE THE WEBSOCKET RUNNING PERIODICALLY IN THE BACKGROUND?
// CAN I PASS THE MESSAGE TO THAT WEBSOCKET ?
// OR SHOULD I CREATE A DIFFERENT WORKER FOR SENDING MESSAGES (e.g.: a OneTimeRequest<SendMessageWorker> for sending messages ?
}
}
From the documentation, I know that you need to build Data objects for passing inputs and so on but there was no example which showcased how to pass input to a worker running periodically in the background.
My experience is saying that you can. Basically you "can't" interact with the worker object via the API. It is really annoying.
For example, with the JS you have the option to get a job and check the parameters of the job. There is no such option with the work. For example, I want to check what is the current state of the restrictions - what is satisfied, what is not. Nothing like this. You can just check states, cancel and that is almost all.
My suggestions is that it is because the WorkManager is a "facade/adapter" over other libraries like JS. It has it's own DB to restore JS jobs on device restart and stuff like this, but beside that if you want to interact with the internals I guess it was just too complicated for them to do so they just skipped.
You can just inject some other object and every time the work can ask it for it's data. I don't see other option.
I am trying out Android WorkManager which is successfully being triggered after every 15 minutes.
However, the work is not being done and I get this error on my logs.
I/WM-WorkerWrapper: Worker result FAILURE for Work
This is how I have set-up my Constraints (Inside the Application Class) to trigger the work.
//set-up work
private fun setUpAsteroidLoadingWork() {
//define work constraints
val workConstraints =
Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(false)
.build()
//create WorkRequest
val workRequest = PeriodicWorkRequestBuilder<LoadAsteroidsWorker>(15, TimeUnit.MINUTES)
.setConstraints(
workConstraints)
.build()
//get WorkManager
val workManager = WorkManager.getInstance(this)
//enqueue work
workManager.enqueueUniquePeriodicWork(
LoadAsteroidsWorker.WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, workRequest)
}
I start the work inside the Application Class onCreate() method
override fun onCreate() {
super.onCreate()
//initialize Timber
Timber.plant(Timber.DebugTree())
Timber.i("Application's onCreate Called")
//start work inside onCreate
runWorkInBackground()
}
//switch work to run on background
private fun runWorkInBackground(){
CoroutineScope(Default).launch {
setUpAsteroidLoadingWork()
}
}
The code is supposed to trigger some work to download internet data in the repository. I have run a #GET request on postman and data is returned with no errors.
This is the Worker Class
class LoadAsteroidsWorker(context: Context, params: WorkerParameters) :
CoroutineWorker(context, params) {
companion object {
const val WORK_NAME = "LoadAsteroidWorker"
}
override suspend fun doWork(): Result {
Timber.i("do workWork() called")
//get instance of database for use with Repo initialization below
val db = AsteroidDatabase.getDatabaseInstance(applicationContext)
//initialize Repo
val repo = AsteroidRepo(db)
return try {
//define work i.e. load asteroids from Network for the next seven days
repo.getAsteroidsFromNetwork()
Timber.i("called repo method")
Result.success()
}catch (e:HttpException){
Timber.i("error - $e")
Result.retry()
}
}
}
This my WorkManager Dependency
//WorkManager - Kotlin + coroutines implementation 'androidx.work:work-runtime-ktx:2.6.0-alpha02'
Any leads on what I am doing wrong?
After a lot of searching, it has dawned on me that the issue is on the doWork() method where I was only 'catching' HttpException forgetting that there are other exceptions to deal with.
I added a second catch block which at last caught the 'bug'.
override suspend fun doWork(): Result {
Timber.i("do workWork() called")
//get instance of database for use with Repo initialization below
val db = AsteroidDatabase.getDatabaseInstance(applicationContext)
//initialize Repo
val repo = AsteroidRepo(db)
return try {
//define work i.e. load asteroids from Network for the next seven days
repo.getAsteroidsFromNetwork()
Timber.i("called repo method")
Result.success()
}catch (e:HttpException){
Timber.i("error - $e")
Result.retry()
}catch (e: Exception){
//catch general exceptions here
Timber.i("exception - $e")
Result.failure()
}
}
The issue had nothing to do with WorkManager but a JsonDataException.
I'm using WorkManager for deferred work in my app.
The total work is divided into a number of chained workers, and I'm having trouble showing the workers' progress to the user (using progress bar).
I tried creating one tag and add it to the different workers, and inside the workers update the progress by that tag, but when I debug I always get progress is '0'.
Another thing I noticed is that the workManager's list of work infos is getting bigger each time I start the work (even if the workers finished their work).
Here is my code:
//inside view model
private val workManager = WorkManager.getInstance(appContext)
internal val progressWorkInfoItems: LiveData<List<WorkInfo>>
init
{
progressWorkInfoItems = workManager.getWorkInfosByTagLiveData(TAG_SAVING_PROGRESS)
}
companion object
{
const val TAG_SAVING_PROGRESS = "saving_progress_tag"
}
//inside a method
var workContinuation = workManager.beginWith(OneTimeWorkRequest.from(firstWorker::class.java))
val secondWorkRequest = OneTimeWorkRequestBuilder<SecondWorker>()
secondWorkRequest.addTag(TAG_SAVING_PROGRESS)
secondWorkRequest.setInputData(createData())
workContinuation = workContinuation.then(secondWorkRequest.build())
val thirdWorkRequest = OneTimeWorkRequestBuilder<ThirdWorker>()
thirdWorkRequest.addTag(TAG_SAVING_PROGRESS)
thirdWorkRequest.setInputData(createData())
workContinuation = workContinuation.then(thirdWorkRequest.build())
workContinuation.enqueue()
//inside the Activity
viewModel.progressWorkInfoItems.observe(this, observeProgress())
private fun observeProgress(): Observer<List<WorkInfo>>
{
return Observer { listOfWorkInfo ->
if (listOfWorkInfo.isNullOrEmpty()) { return#Observer }
listOfWorkInfo.forEach { workInfo ->
if (WorkInfo.State.RUNNING == workInfo.state)
{
val progress = workInfo.progress.getFloat(TAG_SAVING_PROGRESS, 0f)
progress_bar?.progress = progress
}
}
}
}
//inside the worker
override suspend fun doWork(): Result = withContext(Dispatchers.IO)
{
setProgress(workDataOf(TAG_SAVING_PROGRESS to 10f))
...
...
Result.success()
}
The setProgress method is to observe intermediate progress in a single Worker (as explained in the guide):
Progress information can only be observed and updated while the ListenableWorker is running.
For this reason, the progress information is available only till a Worker is active (e.g. it is not in a terminal state like SUCCEEDED, FAILED and CANCELLED). This WorkManager guide covers Worker's states.
My suggestion is to use the Worker's unique ID to identify which worker in your chain is not yet in a terminal state. You can use WorkRequest's getId method to retrieve its unique ID.
According to my analysis I have found that there might be two reasons why you always get 0
setProgress is set just before the Result.success() in the doWork() of the worker then it's lost and you never get that value in your listener. This could be because the state of the worker is now SUCCEEDED
the worker is completing its work in fraction of seconds
Lets take a look at the following code
class Worker1(context: Context, workerParameters: WorkerParameters) : Worker(context,workerParameters) {
override fun doWork(): Result {
setProgressAsync(Data.Builder().putInt("progress",10).build())
for (i in 1..5) {
SystemClock.sleep(1000)
}
setProgressAsync(Data.Builder().putInt("progress",50).build())
SystemClock.sleep(1000)
return Result.success()
}
}
In the above code
if you remove only the first sleep method then the listener only get the progres50
if you remove only the second sleep method then the listener only get the progress 10
If you remove both then the you get the default value 0
This analysis is based on the WorkManager version 2.4.0
Hence I found that the following way is better and always reliable to show the progress of various workers of your chain work.
I have two workers that needs to be run one after the other. If the first work is completed then 50% of the work is done and 100% would be done when the second work is completed.
Two workers
class Worker1(context: Context, workerParameters: WorkerParameters) : Worker(context,workerParameters) {
override fun doWork(): Result {
for (i in 1..5) {
Log.e("worker", "worker1----$i")
}
return Result.success(Data.Builder().putInt("progress",50).build())
}
}
class Worker2(context: Context, workerParameters: WorkerParameters) : Worker(context,workerParameters) {
override fun doWork(): Result {
for (i in 5..10) {
Log.e("worker", "worker1----$i")
}
return Result.success(Data.Builder().putInt("progress",100).build())
}
}
Inside the activity
workManager = WorkManager.getInstance(this)
workRequest1 = OneTimeWorkRequest.Builder(Worker1::class.java)
.addTag(TAG_SAVING_PROGRESS)
.build()
workRequest2 = OneTimeWorkRequest.Builder(Worker2::class.java)
.addTag(TAG_SAVING_PROGRESS)
.build()
findViewById<Button>(R.id.btn).setOnClickListener(View.OnClickListener { view ->
workManager?.
beginUniqueWork(TAG_SAVING_PROGRESS,ExistingWorkPolicy.REPLACE,workRequest1)
?.then(workRequest2)
?.enqueue()
})
progressBar = findViewById(R.id.progressBar)
workManager?.getWorkInfoByIdLiveData(workRequest1.id)
?.observe(this, Observer { workInfo: WorkInfo? ->
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) {
val progress = workInfo.outputData
val value = progress.getInt("progress", 0)
progressBar?.progress = value
}
})
workManager?.getWorkInfoByIdLiveData(workRequest2.id)
?.observe(this, Observer { workInfo: WorkInfo? ->
if (workInfo != null && workInfo.state == WorkInfo.State.SUCCEEDED) {
val progress = workInfo.outputData
val value = progress.getInt("progress", 0)
progressBar?.progress = value
}
})
The reason workManager's list of work infos is getting bigger each time the work is started even if the workers finished their work is because of
workManager.beginWith(OneTimeWorkRequest.from(firstWorker::class.java))
instead one need to use
workManager?.beginUniqueWork(TAG_SAVING_PROGRESS, ExistingWorkPolicy.REPLACE,OneTimeWorkRequest.from(firstWorker::class.java))
You can read more about it here
I just search for code that let me do an action after the timer (with specific date and time) finish (in Kotlin) and save it in a list
Like timer for post a tweet on Twitter:
https://business.twitter.com/en/help/campaign-editing-and-optimization/scheduled-tweets.html
You can use WorkManager for that.
Dependency:
implementation "androidx.work:work-runtime-ktx:2.3.0"
Example:
class LogWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
override fun doWork(): Result {
// Do the work here--in this case, upload the images.
Log.i("ToastWorker", "doWork: Working ⚒ ⚒ ⚒")
// Indicate whether the task finished successfully with the Result
return Result.success()
}
}
Then set the delay time
val logWorkRequest = OneTimeWorkRequestBuilder<LogWorker>()
.setInitialDelay(5, TimeUnit.SECONDS) // here you can set the delay time in Minutes, Hours
.build()
Start the timer
WorkManager.getInstance(this).enqueue(toastWorkRequest)
Here is a Codelab for more insight. You can also read more here