Expo keeps stopping when the app is killed while background location methods

See original GitHub issue

🐛 Bug Report

Summary of Issue

I registered a task for receiving the location updates in the background using Location.startLocationUpdatesAsync(taskName, options), And defined a task in the global scope using TaskManager.defineTask(taskName, task). The app frequently crashes when killed if the background task is active.

  • The Expo client refuses to start unless clearing its storage on Android.

  • The standalone app starts after at least two trials with a crash message.

Environment

  • expo: ^38.0.0
  • expo-task-manager: ~8.3.0
  • expo-location: ~8.2.1

Screenshot

Steps to Reproduce

1- Location.startLocationUpdatesAsync 2- TaskManager.defineTask 3- Kill the application.

Expected Behavior vs Actual Behavior

  • Expected behavior: The running task terminates smoothly.
  • Actual behavior:: The running task is killed, and the app can be restarted without a crash message.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:40 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
roger-schaercommented, Sep 23, 2020

I have tested the background location methods with Expo SDK 39, and I still get the same behavior as before, i.e. the app crashes if it is “swiped” away while the background location task is running. I’ve tested it on emulators with Android 9, 10 & 11, as well as on a real device with Android 11.

I’m really disappointed, as I desperately need this feature to work and had put all my hopes in the new version solving these issues (as described in the CHANGELOG for expo-location / expo-task-manager).

4reactions
Arykcommented, Aug 28, 2020

No problem. Something like this is what i have.

import {useEffect, useRef} from "react";
import haversine from "haversine";

  // In android, sdk 38 managed workflow, we cannot use startLocationUpdatesAsync.
  // So we add a callback in a component to check for changes in the location and send them to the server....
  // works "ok", but will not work when the app is shutoff. We'll have to wait until SDK 39.0 for it to hopefully be solved.
  // https://forums.expo.io/t/startlocationupdatesasync-crashes-app-on-sdk-38-android-managed/42035
  useLocationUpdateForAndroid = () => {
    const lastPosition = useRef<ICoordinates>();
    const locationEnabled = useRef<Boolean>(false);

    // We should not be running useLocationUpdateForAndroid in a place where we don't have permission async, but "just in case", I put this
    // check here.
    useEffect(() => {
      Location.getPermissionsAsync().then(({status}) => {
        if (status === "granted") {
          locationEnabled.current = true;
        }
      });
    }, []);

    const runLocationUpdate = () => locationEnabled.current ? Location.getLastKnownPositionAsync().then(({coords: {latitude, longitude}}) => {
      let updateLocation = true;
      // If they haven't moved more than 15m from last reading don't persist their new location.
      if (lastPosition.current && !haversine({latitude, longitude}, lastPosition.current, {unit: "meter", threshold: 15})) {
        updateLocation = false;
      }
      if (updateLocation) {
        // send to server
      }
      lastPosition.current = {latitude, longitude};
    }) : undefined;

    useEffect(() => {
      const interval = setInterval(() => runLocationUpdate(), 30000);
      return () => clearInterval(interval);
    }, []);
  };

Then you run this in your main app navigator when the user is logged into your app. If they are signed out this component will not render and will not try to send background info to the server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Expo exposes a quit lifecycle method to handle the ...
I was wondering if Expo exposes any lifecycle method to handle when the app is suddenly killed in the background, for instance, when...
Read more >
Location - Expo Documentation
expo -location allows reading geolocation information from the device. Your app can poll for the current location or subscribe to location update events....
Read more >
Expo TaskManager crash when killing app. : r/reactnative
Expo TaskManager crash when killing app. I am running a background task to track locations with Location.startLocationUpdatesAsync().
Read more >
expo-location | Yarn - Package Manager
Allows reading geolocation information from the device. Your app can poll for the current location or subscribe to location update events.
Read more >
AppRegistry - React Native
To "stop" an application when a view should be destroyed, call AppRegistry.unmountApplicationComponentAtRootTag with the tag that was passed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found