如何在不进行序列化的情况下将对象作为输入传递给WorkManager? [英] how to pass object as input to WorkManager without Serialization?
问题描述
我需要将复杂的对象传递给WorkManager.或者我需要序列化包含Livedata和Date的对象.
I need to pass complex object to WorkManager. Or I need to serialize object which contains Livedata and Date.
它抛出java.lang.IllegalArgumentException:关键CabId2具有无效的类型类com.example.sonyadmin.data.Task
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()
推荐答案
WorkManager的数据类仅接受某些特定类型作为值,如参考文档中所述:
WorkManager's Data class only accepts some specific types as values as explained in the reference documentation:
一组持久的键/值对,用作ListenableWorkers的输入和输出.键是字符串,值可以是字符串,原始类型或其数组变体.
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.
最重要的是,其大小限制约为10KB,由常量 WorkManager的代码实验室对此有一个示例,其中将图像的URI传递到WorkRequest中,并且该图像在文件系统上.
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.
这篇关于如何在不进行序列化的情况下将对象作为输入传递给WorkManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!