How to pass Complex Types of Objects to WorkManager in android. i.e lists, maps, POJO

懵懂的女人 提交于 2020-01-13 05:19:06

问题


WorkManager is a library used to enqueue work that is guaranteed to execute after its constraints are met. WorkManager allows observation of work status and the ability to create complex chains of work.

Valid types supported are only: Boolean, Integer, Long, Double, String, and array versions of each of those types.

// Define the Worker class:
public class MathWorker extends Worker {

    // Define the parameter keys:
    public static final String KEY_X_ARG = "X";
    public static final String KEY_Y_ARG = "Y";
    public static final String KEY_Z_ARG = "Z";
    // ...and the result key:
    public static final String KEY_RESULT = "result";

    public MathWorker(
        @NonNull Context context,
        @NonNull WorkerParameters params) {
        super(context, params);
    }

    @Override
    public Worker.Result doWork() {
        // Fetch the arguments (and specify default values):
        int x = getInputData().getInt(KEY_X_ARG, 0);
        int y = getInputData().getInt(KEY_Y_ARG, 0);
        int z = getInputData().getInt(KEY_Z_ARG, 0);

        // ...do the math...
        int result = myCrazyMathFunction(x, y, z);

        //...set the output, and we're done!
        Data output = new Data.Builder()
            .putInt(KEY_RESULT, result)
            .build();
        setOutputData(output);
        return Result.SUCCESS;
    }
}

How can i pass complex types like:

Map<String, Object> studListMap = new HashMap<>();
        studListMap.put("list", studentDetails);

        // Create the Data object:
        @SuppressLint("RestrictedApi")
        Data myData = new Data.Builder()
                .put(KEY_STUD_LIST_ARG,studListMap)
                .build();

        //set network required
        Constraints myConstraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

回答1:


If possible, you may serialize your POJO. For example, if it is truly small and simple, you can use JSON to encode it to string and then decode it in the Worker.

Have used gson, since it's already a dependency in this app.

 Map<String, Object> studListMap = new HashMap<>();
        studListMap.put("list", studentDetails);

        Type listOfStudObject = new TypeToken<List<StudentDetails>>(){}.getType();
        String s = gson.toJson(studentDetails, listOfStudObject);

        // Create the Data object:
        @SuppressLint("RestrictedApi")
        Data myData = new Data.Builder()
                .putString(KEY_STUD_LIST_ARG,s)
                .build();

        //set network required
        Constraints myConstraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        // ...then create and enqueue a OneTimeWorkRequest that uses those arguments
        OneTimeWorkRequest attendWork = new OneTimeWorkRequest.Builder(AddAttendanceWorker.class)
                .setConstraints(myConstraints)
                .setInputData(myData)
                .build();

        WorkManager.getInstance()
                .enqueue(attendWork);



回答2:


Save it in database or you can use JsonStore from IBM, then use rxjava to fetch it, and on successful fetching just call your doWork thing you specified there. Simple.



来源:https://stackoverflow.com/questions/53172079/how-to-pass-complex-types-of-objects-to-workmanager-in-android-i-e-lists-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!