Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass object as input to WorkManager without Serialization?

I need to pass complex object to WorkManager. Or I need to serialize object which contains Livedata and Date.

It throws java.lang.IllegalArgumentException: Key cabinId2 has invalid type class com.example.sonyadmin.data.Task

     val data = workDataOf("cabinId2" to task)
     val uploadWorkRequest = OneTimeWorkRequestBuilder<WManager>()
         .setInputData(data)
         .build()
like image 452
Nurseyit Tursunkulov Avatar asked Apr 27 '19 02:04

Nurseyit Tursunkulov


1 Answers

WorkManager's Data class only accepts some specific types as values as explained in the reference documentation:

A persistable set of key/value pairs which are used as inputs and outputs for ListenableWorkers. Keys are Strings, and values can be Strings, primitive types, or their array variants.

On top of that there's a size limit of about 10KB, specified by the constant MAX_DATA_BYTES.
If the data is not too big, you may want to serialize it to a String and use that as inputData in your WorkRequest. The alternative is to just put a reference of your objects in the inputData. WorkManager's codelab has a sample of this where an URI of an image is passed into a WorkRequest and the image is on the filesystem.

like image 181
pfmaggi Avatar answered Nov 15 '22 10:11

pfmaggi