-
Notifications
You must be signed in to change notification settings - Fork 296
Description
In my app I have integrate live tracking functionality at every 5 seconds I am fetching user's current location in both foreground and background mode which is previously working but since last week it has stopped working even in foreground mode as well it's taking time to fetch location as compare to previous.
Here, is my code
const getLocationWithRetry = async (retries = 3) => {
try {
return await getLocation();
} catch (error) {
if (retries > 0) {
console.log(🔁 Retrying... Attempts left: ${retries}
);
return getLocationWithRetry(retries - 1);
} else {
throw new Error('Failed to get location after multiple attempts');
} } };
const getLocation = async () => {
return new Promise((resolve, reject) => {
Geolocation.getCurrentPosition(
position => resolve(position.coords),
error => {
console.error('Error getting location: ', error);
if (error.code === 3) {
console.log('Location request timed out. Retrying...');
resolve(getLocation());
} else {
reject(error);
}},
{
enableHighAccuracy: true,
// timeout: 20000,
maximumAge: 10000,
}, ); }); };
export const startBackgroundTask = async dispatch => {
console.log('🚀 Starting background service...');
await BackgroundService.start(() => processTasks(dispatch), options);
};
const processTasks = async dispatch => {
while (BackgroundService.isRunning()) {
try {
const location = await getLocationWithRetry();
if (location) {
const {accuracy, latitude, longitude} = location;
console.log('location', location);
if (accuracy > 50) {
console.log('
await sleep(5000);
continue;
} } }
};