Summary
The expo-location API failed to retrieve location data on physical Android devices, despite functioning correctly on iOS devices and Android emulators. The issue stemmed from inconsistent permission handling and background service limitations on Android.
Root Cause
- Permission Granting Discrepancy: Android devices required explicit runtime permissions, which were not consistently granted or handled.
- Background Service Restrictions: Android’s background location access policies were stricter, causing the location service to stop when the app was not in the foreground.
Why This Happens in Real Systems
- Platform-Specific Behavior: Android and iOS handle permissions and background services differently, leading to inconsistencies.
- Emulator vs. Physical Device: Emulators often bypass certain restrictions, masking issues that only appear on real devices.
Real-World Impact
- User Experience: Users on Android devices could not access location-based features, rendering parts of the app unusable.
- Debugging Difficulty: The issue was hard to reproduce and diagnose due to its platform-specific nature.
Example or Code
export function useLocationServices() {
const [currLocation, setCurrLocation] = useState(null);
const [locationError, setLocationError] = useState(null);
const locationSubscription = useRef(null);
useEffect(() => {
async function foregroundTracking() {
try {
let { granted } = await Location.requestForegroundPermissionsAsync();
if (!granted) {
setLocationError("Location permission denied");
return;
}
locationSubscription.current = await Location.watchPositionAsync(
{ accuracy: Location.Accuracy.Balanced, timeInterval: 500, distanceInterval: 1 },
(location) => {
setCurrLocation(location.coords);
}
);
} catch (err) {
setLocationError(err.message);
}
}
foregroundTracking();
return () => {
if (locationSubscription.current) {
locationSubscription.current.remove();
}
};
}, []);
return currLocation;
}
How Senior Engineers Fix It
- Explicit Permission Checks: Add additional checks to ensure permissions are granted before accessing location services.
- Background Permission Requests: Prompt users for background location permissions on Android.
- Error Handling: Implement robust error handling to catch and log permission-related issues.
- Testing on Real Devices: Always test on physical devices to catch platform-specific issues early.
Why Juniors Miss It
- Assumption of Consistency: Juniors often assume that code working on one platform will work on another without considering platform-specific differences.
- Overreliance on Emulators: Emulators do not fully replicate real-device behavior, leading to missed issues.
- Lack of Deep Debugging: Juniors may not dig into platform-specific documentation or logs to identify root causes.