3
votes

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.

1

1 Answers

2
votes

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.