浏览代码

restart location task

Viktoriia 4 月之前
父节点
当前提交
538e790e83
共有 2 个文件被更改,包括 16 次插入1 次删除
  1. 3 1
      src/screens/InAppScreens/MapScreen/index.tsx
  2. 13 0
      src/utils/backgroundLocation.ts

+ 3 - 1
src/screens/InAppScreens/MapScreen/index.tsx

@@ -85,6 +85,7 @@ import _ from 'lodash';
 import ScaleBar from 'src/components/ScaleBar';
 import MessagesDot from 'src/components/MessagesDot';
 import {
+  restartBackgroundLocationUpdates,
   startBackgroundLocationUpdates,
   stopBackgroundLocationUpdates
 } from 'src/utils/backgroundLocation';
@@ -652,7 +653,8 @@ const MapScreen: any = ({ navigation, route }: { navigation: any; route: any })
           await stopBackgroundLocationUpdates();
         }
       } else {
-        await startBackgroundLocationUpdates();
+        // await startBackgroundLocationUpdates();
+        await restartBackgroundLocationUpdates();
       }
 
       try {

+ 13 - 0
src/utils/backgroundLocation.ts

@@ -76,3 +76,16 @@ export const stopBackgroundLocationUpdates = async () => {
     await Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME);
   }
 };
+
+export const restartBackgroundLocationUpdates = async () => {
+  const lastUpdatedVersion = storage.get('location_update_version', StoreType.STRING) ?? '';
+
+  const hasStarted = await Location.hasStartedLocationUpdatesAsync(LOCATION_TASK_NAME);
+  if (hasStarted && lastUpdatedVersion !== APP_VERSION) {
+    await Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME);
+  }
+
+  await startBackgroundLocationUpdates();
+
+  storage.set('location_update_version', APP_VERSION);
+};