我在 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);