Firebase 作业调度程序
Firebase Job Dispatcher
我正在创建一个需要向 API 发送请求的应用程序。此请求必须每 2 分钟发送一次。为此,我使用 Firebase Job Dispatcher
问题是第一个请求是按时发送的,但下一个请求的发送延迟更大:例如。
- 第一个请求:2 分钟后发送
- 第二个请求:2.3 分钟后发送(而不是两分钟)
- 第三个请求:3.5 分钟后发送(而不是两分钟)
- 第四个请求:5 分钟后发送(而不是两分钟)
可想而知,一天后,延迟30分钟甚至更久...
这是我用来创建作业的代码(在主 activity - onCreate 中)
Job pingJob = mDispatcher.newJobBuilder()
.setService(PingJob.class) // the job class to execute
.setTag(PingJob.TAG) //tag - name of the job
.setRecurring(true) // repeat
.setTrigger(Trigger.executionWindow(60, 60*2))
.setReplaceCurrent(false)
.build();
mDispatcher.mustSchedule(pingJob);
这是 PingJob class(为了确保问题不是我在 onStartJob 中所做的,我只放了一个日志):
public class PingJob extends Job {
public static final String TAG = "ping_job";
@Override
public boolean onStartJob(final JobParameters params) {
Log.d("ping job", "just put a log to see when is executed");
jobFinished(params, true);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}
我正在使用一个 Android 框和 Android 6.0.1(但我的应用程序也必须 运行 在 4.3 设备上,这就是为什么我使用 firebase 而不是 JobScheduler ) 和 Google 播放服务 11.5.09 (434)。
你知道它为什么这样做吗?如果有办法解决这个问题?如果不是,是否有其他方法可以每 2 分钟完成一次工作?
你没有做错任何事。这是设计好的预期行为。 FirebaseJobDispatcher 在 Google Play Services 中使用调度引擎,可能会使用 JobScheduler under the hood for Android 5.0 and higher. JobScheduler batches tasks from different apps into one batch to not wake up a device too often and save a battery. So there's no guarantee that your task will be executed exactly every 2 minutes. If you need this precision consider using AlarmManager,不建议进行任何网络调用,因为它会影响电池寿命。
你为什么要这么频繁地发送请求?考虑多想想你在做什么。将发送到后端的数据保存在数据库或磁盘中可能会更好。然后,您可以每 30-60 分钟或任何适合您的时间分批发送一次。您还可以使用 Firebase Cloud Messaging 向设备发送消息,要求它在需要时将数据发送到后端。
我正在创建一个需要向 API 发送请求的应用程序。此请求必须每 2 分钟发送一次。为此,我使用 Firebase Job Dispatcher
问题是第一个请求是按时发送的,但下一个请求的发送延迟更大:例如。
- 第一个请求:2 分钟后发送
- 第二个请求:2.3 分钟后发送(而不是两分钟)
- 第三个请求:3.5 分钟后发送(而不是两分钟)
- 第四个请求:5 分钟后发送(而不是两分钟)
可想而知,一天后,延迟30分钟甚至更久...
这是我用来创建作业的代码(在主 activity - onCreate 中)
Job pingJob = mDispatcher.newJobBuilder()
.setService(PingJob.class) // the job class to execute
.setTag(PingJob.TAG) //tag - name of the job
.setRecurring(true) // repeat
.setTrigger(Trigger.executionWindow(60, 60*2))
.setReplaceCurrent(false)
.build();
mDispatcher.mustSchedule(pingJob);
这是 PingJob class(为了确保问题不是我在 onStartJob 中所做的,我只放了一个日志):
public class PingJob extends Job {
public static final String TAG = "ping_job";
@Override
public boolean onStartJob(final JobParameters params) {
Log.d("ping job", "just put a log to see when is executed");
jobFinished(params, true);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}
我正在使用一个 Android 框和 Android 6.0.1(但我的应用程序也必须 运行 在 4.3 设备上,这就是为什么我使用 firebase 而不是 JobScheduler ) 和 Google 播放服务 11.5.09 (434)。
你知道它为什么这样做吗?如果有办法解决这个问题?如果不是,是否有其他方法可以每 2 分钟完成一次工作?
你没有做错任何事。这是设计好的预期行为。 FirebaseJobDispatcher 在 Google Play Services 中使用调度引擎,可能会使用 JobScheduler under the hood for Android 5.0 and higher. JobScheduler batches tasks from different apps into one batch to not wake up a device too often and save a battery. So there's no guarantee that your task will be executed exactly every 2 minutes. If you need this precision consider using AlarmManager,不建议进行任何网络调用,因为它会影响电池寿命。
你为什么要这么频繁地发送请求?考虑多想想你在做什么。将发送到后端的数据保存在数据库或磁盘中可能会更好。然后,您可以每 30-60 分钟或任何适合您的时间分批发送一次。您还可以使用 Firebase Cloud Messaging 向设备发送消息,要求它在需要时将数据发送到后端。