• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps OneTimeWorkRequest: After setting the input merger to ArrayCreatingInputMerger, the code inside doWo

mcpixel

Newbie
This main code:
Java:
package com.example.app3;

import androidx.appcompat.app.AppCompatActivity;
import androidx.work.ArrayCreatingInputMerger;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

import android.os.Bundle;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OneTimeWorkRequest o1 = new OneTimeWorkRequest.Builder(MyWorker.class)
                .setInputData(new Data.Builder().putString("name", "number1")
                        .build())
                .build();
        OneTimeWorkRequest o2 = new OneTimeWorkRequest.Builder(MyWorker.class)
                .setInitialDelay(2, TimeUnit.SECONDS)
                .setInputData(new Data.Builder().putString("name", "number2")
                        .putInt("number", 2).build())
                .build();
        OneTimeWorkRequest o3 = new OneTimeWorkRequest.Builder(MyWorker.class)
                .setInputMerger(ArrayCreatingInputMerger.class)
                .setInputData(new Data.Builder().putString("name", "number3")
                        .build())
                .setConstraints(new Constraints.Builder().setRequiresCharging(true).setRequiresBatteryNotLow(true).build())
                .setInitialDelay(2, TimeUnit.SECONDS).build();

        WorkManager.getInstance(getApplicationContext())
                .beginWith(Arrays.asList(o1, o2))
                .then(o3)
                .enqueue();
    }
}
MyWorker.java:
Java:
package com.example.app3;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

public class MyWorker extends Worker {
    public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        int number = getInputData().getInt("number", 0);
        Data ret = new Data.Builder().putInt("number", number*2).build();
        Log.e(getInputData().getString("name"), ""+number*2);
        return Result.success(ret);
    }
}
After starting the application, I see the following in logcat:
Code:
08-03 23:57:16.188 26949-26968/com.example.app3 E/number1: 0
08-03 23:57:16.226 26949-26965/com.example.app3 I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=ec6086ea-b8d4-41e6-9bff-936fe2c157b3, tags={ com.example.app3.MyWorker } ]
08-03 23:57:18.046 26949-26974/com.example.app3 E/number2: 4
08-03 23:57:18.047 26949-26964/com.example.app3 I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=93a8312e-aa31-41d0-97b2-35d4d49668db, tags={ com.example.app3.MyWorker } ]
08-03 23:57:18.050 26949-26964/com.example.app3 I/WM-WorkerWrapper: Setting status to enqueued for a0fc4f5c-a9da-4bfd-b91a-428e5c67c56b
08-03 23:57:20.076 26949-26965/com.example.app3 I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=a0fc4f5c-a9da-4bfd-b91a-428e5c67c56b, tags={ com.example.app3.MyWorker } ]

What could be the problem? It seems to me that something is wrong with the code. I tried to do this:
Java:
if("number3".equals(getInputData().getString("name"))) {
            int[] nums = getInputData().getIntArray("number");
            assert nums != null;
            Log.e("TEST", "" + nums.length);
        }
But nothing happened because the code just doesn’t want to run and nothing is clear from the documentation
 
You know you can help yourself a lot by running your app in debug mode, and setting breakpoints. Step through the code line by line, to trace exactly what it's doing.
 
Back
Top Bottom