fluttercommunity / flutter_workmanager

A Flutter plugin which allows you to execute code in the background on Android and iOS.
852 stars 261 forks source link

Improved iOS BGTaskScheduler request options and scheduling #295

Open walsha2 opened 3 years ago

walsha2 commented 3 years ago

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

ened commented 3 years ago

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

On BGProcessingTask vs BGAppRefreshTask:

About "Continuous Scheduling":

jocelyngriselle commented 3 years ago

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

bettysteger commented 3 years ago

@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)?

wilsonrm commented 1 year ago

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.

absar commented 1 year ago

@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 commented 1 year ago

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

ened commented 1 year ago

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