flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

Improved iOS BGTaskScheduler request options and scheduling

Open walsha2 opened this issue 4 years ago • 7 comments

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

walsha2 avatar Jul 14 '21 18:07 walsha2

Hi @walsha2 & thank you for the issue. You raise many interesting points.

On BGProcessingTask vs BGAppRefreshTask:

  • I was not aware at the time of the differences and the impact on the scheduler for these. This should definitely be a improvement to the plugin. I think it can be addressed by extending the .registerOneOffTask task and pass a parameter.

About "Continuous Scheduling":

  • Hm, I am not fully sure about that one. I thought that we could schedule further tasks from within the Workmanager dispatcher (registered via initialize). When the work within that task requires other tasks, it should be straightforward to schedule from there. What do you think?

ened avatar Jul 15 '21 20:07 ened

Hello are you interested in any help on this matter ? I'm willing to help in order to have a registerPeriodicTask working in iOS, seem like we could definitly use BGAppRefreshTask althought frequency will not be guaranteed. Cheers

jo11yn avatar Aug 13 '21 13:08 jo11yn

@jocelyngriselle that would be awesome! 1 other question: is there a guarantee, that the BGAppRefreshTask runs let say daily, if the app was not open a longer time (on iOS 13)?

bettysteger avatar Sep 17 '21 14:09 bettysteger

Hi @walsha2 & thank you for the issue. You raise many interesting points.

On BGProcessingTask vs BGAppRefreshTask:

  • I was not aware at the time of the differences and the impact on the scheduler for these. This should definitely be a improvement to the plugin. I think it can be addressed by extending the .registerOneOffTask task and pass a parameter.

About "Continuous Scheduling":

  • Hm, I am not fully sure about that one. I thought that we could schedule further tasks from within the Workmanager dispatcher (registered via initialize). When the work within that task requires other tasks, it should be straightforward to schedule from there. What do you think?

Hi, is there a plan to implement this please? That is to be able to submit to the BGTaskScheduler both a BGAppRefreshTaskRequest & BGProcessingTaskRequest.

wilsonrm avatar Oct 31 '22 10:10 wilsonrm

@ened can you please update us on the the issue @walsha2 were you able to find a solution for BGAppRefreshTask @jocelyngriselle were you able to fork or create another solution for iOS period tasks and BGAppRefreshTask

absar avatar Sep 11 '23 15:09 absar

Hi @ened I've been working on improving iOS side to incorporate BGAppRefreshTask, one off immediate tasks, and processing tasks, and improving iOS docs, if you are not planning on this issue, it would be nice to collaborate and merge into workmanager, since iOS side of this plugin is lacking too much compared to Android. So far looks like I will be able to finish this week

absar avatar Sep 24 '23 22:09 absar

@absar please open the PR so I can start taking a look. I was working on macOS next and want to prevent merge conflicts.

ened avatar Sep 27 '23 10:09 ened