Description
@ened I have been reviewing the work completed in #243 and working with the latest version of this package that includes those changes. I have some comments and questions that warranted a new issue and discussion, summarized here.
BGProcessingTask
vs BGAppRefreshTask
Is there a reason that this was implemented entirely as a BGProcessingTaskRequest
? Is there any plan to allow for this request to be a BGAppRefreshTaskRequest
to handle smaller, more bite size operations? BGProcessingTaskRequest
is intended for tasks that take "minutes" and is overkill for more trivial background tasks. It may be detrimental to iOS scheduling to use this request type when it is not necessary.
I am not sure if iOS has greater restrictions on the number of times BGProcessingTask
can be executed vs BGAppRefreshTask
. I assume that BGAppRefreshTask
can be executed and scheduled more often per iOS guidelines. I am trying to use this in order to perform home screen widget background refresh tasks and these are fairly lightweight operations so it makes more sense to use a BGAppRefreshTaskRequest
and not get penalized by the iOS scheduler.
Continuous Scheduling
Second, it does not seem like there is a way to schedule these operations in a continuous manner. This is actually the purpose of BGAppRefreshTask
and this is clearly displayed by Apple in the following link and code example, by Apple:
If you download that code example you will see the following:
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.apple", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
Then later on:
func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: "com.example.apple")
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // Fetch no earlier than 15 minutes from now
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
func handleAppRefresh(task: BGAppRefreshTask) {
scheduleAppRefresh()
....
lastOperation.completionBlock = {
task.setTaskCompleted(success: !lastOperation.isCancelled)
}
queue.addOperations(operations, waitUntilFinished: false)
}
Do you notice how handleAppRefresh()
calls scheduleAppRefresh()
so that this task can be continuously scheduled?This is perfect for things like home screen widgets and the purpose of BGProcessingTask
. Right now, there are two limitations with flutter_workmanager
:
- Background tasks cannot be assigned as
BGAppRefreshTask
- There is no way to control the continuous scheduling because this is not accessible by
SwiftWorkmanagerPlugin
Even if I wanted to use BGProcessingTask
(as it is currently implemented), SwiftWorkmanagerPlugin
does not expose a hook in the SwiftWorkmanagerPlugin.handle()
method to kick off another scheduled task after the current task completes.
Discussion
Please let me know if that makes sense or if you have any questions! I think this would be great capability to add and is inline with how iOS intends BGTaskScheduler
to be used. Look forward to discussing further!