如何在 Android Jetpack Workmanager 中输入和输出 AN 对象数据?

How to input and output A Object data in Android Jetpack Workmanger?

在AndroidJetpack WorkManger中,在Worker中定义一个输出数据:

new Data.Builder().putLong(xxx);

putLong, putString, putBoolean,但没有定义函数来放置 Object

如何在 Worker 中输出 a Entity Object

public class DownloadDB extends Worker {
    private static final String TAG = "DownloadDB";
    private List<ChoiceTestItem> mChoiceTestItems;

    public DownloadDB(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        downChoiceTestItemFromNetwork();
        HashMap<String, Object> map = new HashMap<>();

        //Wrong Code here!!!
        map.put("a", mChoiceTestItems);
        @SuppressLint("RestrictedApi") Data data = new Data.Builder().putAll(map).build();

        return Result.success(data);
    }
...

there are not define a function to put a Object.

正确。您不能将任意对象放入 Data。正如 the documentation 所述:"This is a lightweight container, and should not be considered your data store"。不允许任意数据类型是他们强制执行此操作的一种方式。

How can i output a Entity Object in Worker.

如果 "Entity Object" 是指房间 @Entity,请将该实体放入您的数据库中。将主键或其他标识符放入 Data,稍后使用它从数据库中检索实体。