Skip to content

Improved iOS BGTaskScheduler request options and scheduling #295

Open
@walsha2

Description

@walsha2

@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:

https://developer.apple.com/documentation/backgroundtasks/refreshing_and_maintaining_your_app_using_background_tasks

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:

  1. Background tasks cannot be assigned as BGAppRefreshTask
  2. 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions