本文介绍了如何在不进行序列化的情况下将对象作为输入传递给WorkManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将复杂的对象传递给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:

最重要的是,其大小限制约为10KB,由常量 MAX_DATA_BYTES .
如果数据不是太大,则可能需要将其序列化为String并将其用作WorkRequest中的inputData.另一种方法是只将对象的引用放入inputData中. 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:14