Flutter Backgroundfetch on iOS doesn't fire
Asked Answered
A

1

6

I'm trying to develop an app with flutter that uses the background_fetch package https://pub.dev/packages/background_fetch to connect within a specific time period to a BLE device and receives some status data. On Android, everything works fine and the background fetch is fired reliably every x minutes.

On iOS it also works as long as I simulate a background fetch manually through Xcode. However, when I run my app on an iPhone, the background fetch is never fired. For me, it's pretty hard to understand if I have an error in my code or if iOS really never fires a background fetch. If the os never fires a background fetch, how am I able to trigger one?

What I understood from other posts is that as long as the background fetch works in the simulator, everything is programmed correctly.

Can somehow explain to me in an easy way how the background fetch mechanism works on iOS?

thank you and best regards.

Aphrodisia answered 24/3, 2021 at 14:22 Comment(0)
A
4

I would like to share some points I'm starting to understand while trying to solve this issue:

Even if they say on the package website that iOS does not support scheduled tasks, I did not really understand, what that means, because the concept of using background tasks wasn't or still isn't 100% clear to me.

As far as I understand, it basically means that the only background fetch mechanism that iOS supports, is the DEFAULT background fetch that occurs in a specific time period defined by the minimumFetchInterval parameter. This can be a minimum of 15 minutes or longer, but never less than 15 minutes, and is implemented with the following snippet from the example:

int status = await BackgroundFetch.configure(BackgroundFetchConfig(
        minimumFetchInterval: 15,
        stopOnTerminate: false,
        enableHeadless: true,
        requiresBatteryNotLow: false,
        requiresCharging: false,
        requiresStorageNotLow: false,
        requiresDeviceIdle: false,
        requiredNetworkType: NetworkType.NONE
    ),

However, you are not able to really test the fetch event - not with the simulator nor with a real device. With Xcode, you can simulate a background fetch by firing it from the IDE. But a background fetch will NEVER happen in the simulator by itself after the defined time period. It will neither occur if you connect a real iPhone and deploy your app on this device. As far as I understood, the app will run in another lifecycle (whatever that exactly means), in which the app will not receive time to execute background tasks.

Still, I'm not able to understand how or when exactly I can observe my apps behaviour on a real device if iOS doesn't schedule background execution time for my app. Can I be sure, that if it works on the simulator, it also works on a real device?

It would be great if someone with a better understanding can help with that.

Aphrodisia answered 26/3, 2021 at 17:57 Comment(3)
What I need to achieve, is that I want to upload files to a server at 3 am. As more as I understand about background tasks, it seems tome, that they are not reliably enough (at least on iOS). I also tested the workmanager package pub.dev/packages/workmanager which shows similar behavior. I also tested the cron package pub.dev/packages/cron, but cron jobs defined by this package stop executing after a couple of seconds as soon as the app goes into background (only on iOS). Does anyone has another idea how I can achieve what I want to do?Aphrodisia
Did you ever find a working solution to running background fetch on ios when in release builds? I'm having the same issue.Becharm
Has Anyone found a solution to that?Grundy

© 2022 - 2024 — McMap. All rights reserved.