如何在不进行序列化的情况下将对象作为输入传递给WorkManager?

编程入门 行业动态 更新时间:2024-10-27 09:43:57
本文介绍了如何在不进行序列化的情况下将对象作为输入传递给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:

一组持久的键/值对,用作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,由常量 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?

本文发布于:2023-11-06 14:46:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   对象   序列化   如何在   WorkManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!