React-native-geolocation-service: Cannot request gps geolocation twice in parallel.

Created on 5 Oct 2018  路  3Comments  路  Source: Agontuk/react-native-geolocation-service

I'm working on a large react-native app with some legacy code in it from previous developers. Originally, we were using react-native's default geolocation which was slow on android/would timeout, but mostly worked.

As part of our backend api, we need to send up to date geolocation data on each request we make to our server, so we wrote a middleware that would automatically query for our location, and merge it into our request headers with the react-native navigator.geolocation API.

To speed things up, we researched, and found this package, and implemented it. We saw a 100% improvement in geolocation on android, which brought it up to par with the speeds we were seeing on Android, so huge thanks for your work and integration.

However, we found a bug. We have a part of our app that did a Promise.all([request1, request2]), which consistently only let one request through. After some investigation, we realized it was geolocation that was preventing then second query.

I'm not familiar enough with Android development, or the fused location service, but I'm guessing there is some unhandled error here. We fixed it on our javascript side by storing the current geolocation queries promise, and clearing it after the query is complete. Then if another parallel geolocation request comes it, we reuse the query promise if it is available.

Ultimately, I'm not sure if there is a fix for this on the java side, or if at least the documentation for this package could make a note on parallel requests.

Most helpful comment

I just encountered this issue on my project.

I am calling calling the function Geolocation.getCurrentPosition three times simultaneously, and only one of my request is beeing processed, the two others don't execute the success callback, neither the error callback.

If concurrent calls are not allowed, I think that the real issue is that the error callback is not called in that case.

After debugging, I realised that in the two failing cases, the two variables mSuccessCallback and mErrorCallback are initialised to null at the beginning of getCurrentPosition in RNFusedLocationModule.java, probably because of the clearCallbacks(); call after the invokeSuccess of the first request.

@Agontuk ,
To prevent this, would that make sense to you to check if mSuccessCallback is not already defined at the beginning of getCurrentPosition, and execute invokeError if it is ? I could make a PR for that.

Thanks

All 3 comments

Location request is synchronous, even if you request location in parallel the native implementation will process one request at a time. This was done to avoid illegal callback invocation exception.

Since parallel location request will return the same co-ordinates almost all the time, I think it would be better to cache it on client side like you did in your app.

Closing due to inactivity.

I just encountered this issue on my project.

I am calling calling the function Geolocation.getCurrentPosition three times simultaneously, and only one of my request is beeing processed, the two others don't execute the success callback, neither the error callback.

If concurrent calls are not allowed, I think that the real issue is that the error callback is not called in that case.

After debugging, I realised that in the two failing cases, the two variables mSuccessCallback and mErrorCallback are initialised to null at the beginning of getCurrentPosition in RNFusedLocationModule.java, probably because of the clearCallbacks(); call after the invokeSuccess of the first request.

@Agontuk ,
To prevent this, would that make sense to you to check if mSuccessCallback is not already defined at the beginning of getCurrentPosition, and execute invokeError if it is ? I could make a PR for that.

Thanks

Was this page helpful?
0 / 5 - 0 ratings