doWork() method is not getting called from Work Manager
Asked Answered
H

1

7

I am trying to make server call using WorkManager. So, I wrote the below Worker class:

class ServerCallWorker extends Worker {
        public ServerCallWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
            super(context, workerParams);
            Log.d("Testing", "ServerCallWorker constructor method" );
        }

        @NonNull
        @Override
        public Worker.Result doWork() {
            Log.d("Testing", "doWork() method" );
            return Worker.Result.SUCCESS;
        }
    }

This worker class I am calling from my onCreate() method of my activity. The code snippet is here:

 private void getLocationUpdate() {
        Log.d("Testing", "inside Location Update");
        WorkManager workManager = WorkManager.getInstance();
        Log.d("Testing", "inside Location Update 222");
        Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType
                .CONNECTED).build();
//        PeriodicWorkRequest recurringWork = new PeriodicWorkRequest.Builder(FilterWorker.class, 15,
//                TimeUnit.MINUTES).build();
        OneTimeWorkRequest wrkReq = new OneTimeWorkRequest.Builder(ServerCallWorker.class).setConstraints(constraints)
                .build();
        Log.d("Testing", "inside Location Update 333");
        workManager.enqueue(wrkReq);
        Log.d("Testing", "inside Location Update 444");
    }

Now, getLocationUpdate() is getting called and it is printing the logs. But doWork() method is not getting called as no logs are showing. Same for ServerCallWorker() method.

Can someone please point out, where I am making wrong.

Thanks, Arindam.

Hutch answered 12/11, 2018 at 19:25 Comment(7)
Make sure the device is connected to the internet when getLocationUpdate() is called. Otherwise ServerCallWorker() will be scheduled to be called on device internet connection.Intendance
I am connected with internet. If I remove the Network related constraint, still it is not working. doWorK() is not getting called.Hutch
@ArindamMukherjee have you solved this problem? I am also facing the same problem.Aftmost
I am facing the same issue, Did you find a solution?Minestrone
I have same issue in nougat.Anthropopathy
Any update on this? I am also facing this issue. I doesn't happen always but happens sometimes.Wrigley
have you found out a solution?Oneness
P
0

Might not be your case, but when you are setting data using setInputData(), if the payload/data object is too large, doWork() may not get called.

Photoelectron answered 23/4, 2019 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.