As stated in this answer here
You will need to pass them by reference. So if you have a Room Entity grab its key / id and pass it as an input to the Worker by using setInputData in {OneTime|Periodic}WorkRequest.Builder and you can then query for the actual Room Entity in the implementation of the Worker.
in my case, I used it to get a value from the database and perform a task, but it is the same principle.
@Override
public Result doWork() {
final Context applicationContext = getApplicationContext();
try {
final String title = getInputData().getString(Constants.TITLE);
final String msg = getInputData().getString(Constants.MESSAGE);
final int id = getInputData().getInt(Constants.ID,0);
Executor myExecutor = Executors.newSingleThreadExecutor();
myExecutor.execute(new Runnable() {
@Override
public void run() {
Entity entity = BasicApp.getInstance().getDatabase().entityDao().getEntity(id);
Timber.tag(TAG).d( "id for Entity %s", entity.getId());
Utils.doMyTask(new String[] {title, msg}, id, applicationContext);
}
});
return Result.success();
} catch (Throwable throwable) {
// Technically WorkManager will return Result.failure()
// but it's best to be explicit about it.
// Thus if there were errors, we're return FAILURE
Timber.tag(TAG).e(throwable, "Error scheduling notification");
return Result.failure();
}
}
Create a WorkRequest for the Work manager to do your task
public void do my task(long delay) {
OneTimeWorkRequest request =
new OneTimeWorkRequest.Builder(MyWorker.class)
.setInputData(createInputData())
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
.addTag(Constants.MY_WORK_NAME)
.build();
mWorkManager.enqueue(request);
}
Creates an input data bundle which includes the values
@return Data which contains the values details
private Data createInputData() {
Data.Builder builder = new Data.Builder();
builder.putString(Constants.TITLE, title);
builder.putString(Constants.MESSAGE, msg);
builder.putInt(Constants.ID,id);
return builder.build();
}