In Android Jetpack WorkManger, define a output data in Worker:
new Data.Builder().putLong(xxx);
there are putLong, putString, putBoolean
, BUT there are not define a function to put a Object
.
How can i output a Entity Object
in Worker.
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.
Correct. You cannot put arbitrary objects into a Data
. As the documentation states: "This is a lightweight container, and should not be considered your data store". Not allowing arbitrary data types is one way they are enforcing this.
How can i output a Entity Object in Worker.
If by "Entity Object" you mean a Room @Entity
, put the entity in your database. Put the primary key or other identifier into the Data
, and use that to retrieve the entity at a later point from the database.