icapps / flutter-background-location-tracker

Flutter background location tracker plugin. (Android / iOS)
MIT License
31 stars 35 forks source link

Callback function stops executing after a few instructions, no error message is displayed #48

Closed dJani97 closed 1 year ago

dJani97 commented 1 year ago

Hi!

I've been chasing this problem for a while so I thought I'll post it here.

I'm working on a location-tracker app that will record the user's position in the background, and do a couple of things:

  1. save the location locally (in shared preferences, or to a file) so it can be used later in the foreground
  2. when online, send the location to the backend

I'm running into a weird problem while developing this app. The callback function starts running, executes a couple of instructions, and then randomly halts without any error message.

The weird thing is, I've tried running the debugger to see if there are any uncaught exceptions, and with the debugger attached, the entire callback function is executed just fine! No errors. It does what it should.

But once I detach the debugger and just simply use flutter run, execution stops about halfway through the callback.

I'm not doing anything resource-intensive, there are a couple of read/write operations and a call to a REST API, but the entire thing shouldn't take more than 50-100ms, and most of this is asynchronous waiting.

Since it is not consistent, my only thought is that there may be a time limit, maybe only a couple of milliseconds allowed for the callback. Is this scenario possible? If it is, how could I execute more complex operations, like caching and/or sending the locations over HTTP?

An oversimplified version of my code looks kinda like this:

@pragma('vm:entry-point')
Future<void> backgroundCallback() async {
  BackgroundLocationTrackerManager.handleBackgroundUpdated((BackgroundLocationUpdateData data) async {

    final user = await readAuth(); // read the authenticated user's data from storage

    if (user.authenticated) {
        final locations = await readStoredLocations();
        locations.add(data);
        await saveLocations(locations);

       try {
            await sendLocationToBackend(user.token, data);
       } catch (e) {
            debugPrint('**** background_location_tracker error while sending data: $e');
       }
    }
  );
}

Maybe it is happening because of all the async operations? Maybe I'm not properly awaiting execution somewhere?

NicolaVerbeeck commented 1 year ago

Android or iOS?

dJani97 commented 1 year ago

Forgot to include details!

Yes, I'm running on Android with the following dependency versions:

Flutter 3.7
Dart 2.19.0
background_location_tracker: ^1.4.0
NicolaVerbeeck commented 1 year ago

Yeah this is something we see on android devices not directly from Google. We request the correct flags to keep things running in background, but the OEMs decide they won't follow the rules. (looking at you oneplus for not allowing the clock app to run in the background and thus causing me to miss alarms).

Can you try to exclude the app from battery optimisations? That seems to work for us