问题
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()
回答1:
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.
来源:https://stackoverflow.com/questions/55876824/how-to-pass-object-as-input-to-workmanager-without-serialization