how to pass object as input to WorkManager without Serialization?

江枫思渺然 提交于 2020-01-24 19:54:10

问题


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

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