gpt4 book ai didi

java - Android 工作管理器 : Cannot get output data from PeriodicWorkRequest

转载 作者:行者123 更新时间:2023-12-02 09:27:22 26 4
gpt4 key购买 nike

由于某些原因,我无法从 Android WorkManager 的PeriodicWorkRequest 获取输出数据。工作线程按预期定期运行,只是不返回任何数据。

我的主要 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
schedulePeriodicRequests();
}

public void schedulePeriodicRequests() {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(ServerRequestsWorker.class, 15, TimeUnit.MINUTES)
.build();

WorkManager workManager = WorkManager.getInstance(MainActivity.this);
workManager.enqueue(workRequest);
workManager.getWorkInfoByIdLiveData(workRequest.getId())
.observe(this, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
Log.d(LOG_TAG, "WorkInfo received: state: " + workInfo.getState());
String message = workInfo.getOutputData().getString(ServerRequestsWorker.KEY_MESSAGE);
Log.d(LOG_TAG, "message: " + message);
}
}
});
}

我的ServerRequestsWorker:

public static final String KEY_MESSAGE = "message";

@NonNull
@Override
public Result doWork() {
Log.d(LOG_TAG, "Worker works");

Data outputData = new Data.Builder()
.putString(KEY_MESSAGE, "This is output message")
.build();

return Result.success(outputData);
}

workInfo.getOutputData().getString 的值始终为 null。这是我在日志中得到的内容:

MainActivity: WorkInfo received: state: ENQUEUED
MainActivity: message: null
ServerRequestsWorker: Worker works
WM-WorkerWrapper: Worker result SUCCESS for Work [ id=5f2beba8-c8bf-4f07-b4ee-e876e95d3cdb, tags={ com.anshmidt.pricemonitor.ServerRequestsWorker } ]
MainActivity: WorkInfo received: state: RUNNING
MainActivity: message: null
MainActivity: WorkInfo received: state: ENQUEUED
MainActivity: message: null

有趣的是,从 OneTimeWorkRequest 获取输出数据工作正常。如果我从PeriodicWorkRequest切换到OneTimeWorkRequest:

    OneTimeWorkRequest serverScraperWorkRequest = new OneTimeWorkRequest.Builder(ServerRequestsWorker.class)
.build();

WorkManager workManager = WorkManager.getInstance(MainActivity.this);
workManager.enqueue(serverScraperWorkRequest);
workManager.getWorkInfoByIdLiveData(serverScraperWorkRequest.getId())
.observe(this, new Observer<WorkInfo>() {
@Override
public void onChanged(@Nullable WorkInfo workInfo) {
if (workInfo != null) {
Log.d(LOG_TAG, "WorkInfo received: state: " + workInfo.getState());
String message = workInfo.getOutputData().getString(ServerRequestsWorker.KEY_MESSAGE);
Log.d(LOG_TAG, "message: " + message);
}
}
});

,然后我成功接收到输出数据的值:

MainActivity: message: This is output message

我尝试过将独特的工作排队并按标签排队,但没有运气。尝试过WorkManager 2.2.0和1.x,结果是一样的。

我错过了什么吗?是否有可能从PeriodicWorkRequest 获取输出数据?我做错了什么?

最佳答案

根据 WorkInfo documentation :

Note that output is only available for the terminal states (WorkInfo.State.SUCCEEDED and WorkInfo.State.FAILED).

对于定期工作,每次运行都会重复使用相同的 ID,这意味着工作永远不会达到 SUCCEEDED - 相反,工作会立即转换回 ENQUEUED,等待用于下一次定期运行。因此,您所经历的似乎是预期的行为。

正如您所见,使用一次性工作不会出现这种行为,因为该工作实际上已成功SUCCEEDED并且每次后续运行(如果您的一次性工作是重新安排相同类型的工作) ) 将有一个新的 ID。另一种选择是使用输出数据,而是将定期工作的结果存储在您自己的数据库中。

关于java - Android 工作管理器 : Cannot get output data from PeriodicWorkRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57845459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com