0

我正在使用 WorkManager 安排一些定期工作,以将本地数据库与远程数据库同步。几个月来一切正常,直到有一天数据库停止同步。我通过 Stetho 发现我所有的 Work 实例的“schedule_requested_at”值都等于 -1。

PeriodicWorkRequest refreshCpnWork = new PeriodicWorkRequest.Builder(MainWorker.class, Constants.WORKER_INTERVAL_MIN, TimeUnit.MINUTES)
    .addTag("PeriodicWork")
    .build();

WorkManager.getInstance().enqueue(refreshCpnWork);

这些是正常情况下的预定工程

这就是我安排工作的方式,正如我所说的那样,几个月来它工作得很好。但是从某个时刻开始,它停止了安排我的工作,并且在图片中,而不是正确的时间戳是-1。

4

1 回答 1

0

我在 schedule_requested_at 中发现了 -1 的含义。

来自 WorkSpec.java (work-runtime:1.0.0-alpha11)

/**
 * This field tells us if this {@link WorkSpec} instance, is actually currently scheduled and
 * being counted against the {@code SCHEDULER_LIMIT}. This bit is reset for PeriodicWorkRequests
 * in API < 23, because AlarmManager does not know of PeriodicWorkRequests. So for the next
 * request to be rescheduled this field has to be reset to {@code SCHEDULE_NOT_REQUESTED_AT}.
 * For the JobScheduler implementation, we don't reset this field because JobScheduler natively
 * supports PeriodicWorkRequests.
 */
@ColumnInfo(name = "schedule_requested_at")
public long scheduleRequestedAt = SCHEDULE_NOT_REQUESTED_YET;

如果值为 -1,则工作已插入到表中,但尚未安排。

在我的情况下,有太多的 Work 实例来安排新的事情,这个查询来自 WorkSpecDao.java

/**
 * @return The List of {@link WorkSpec}s that are eligible to be scheduled.
 */
@Query("SELECT * FROM workspec WHERE "
        + "state=" + WorkTypeConverters.StateIds.ENQUEUED
        // We only want WorkSpecs which have not been previously scheduled.
        + " AND schedule_requested_at=" + WorkSpec.SCHEDULE_NOT_REQUESTED_YET
        + " LIMIT "
            + "(SELECT MAX(:schedulerLimit" + "-COUNT(*), 0) FROM workspec WHERE"
                + " schedule_requested_at<>" + WorkSpec.SCHEDULE_NOT_REQUESTED_YET
                + " AND state NOT IN " + COMPLETED_STATES
            + ")"
)
List<WorkSpec> getEligibleWorkForScheduling(int schedulerLimit);
于 2019-06-17T07:09:29.990 回答