ground-android icon indicating copy to clipboard operation
ground-android copied to clipboard

OfflineBaseMaps: Exception android.os.NetworkOnMainThreadException

Open scolsen opened this issue 4 years ago • 6 comments
trafficstars

Apparently we're executing network instructions on the main thread. Weirdly enough, this exception never occurred when I committed the recent networking changes to offline basemaps...but now it does.

scolsen avatar Sep 23 '21 16:09 scolsen

When I attempt to fix this issue by setting the stream to the IO thread, I get the following exception instead:

com.google.maps.api.android.lib6.common.apiexception.c: Not on the main thread

scolsen avatar Sep 23 '21 16:09 scolsen

I'm guessing this might have something to do with the recent changes to the map? I think what's happening is the following:

RX stream 1 (offline basemap repo) --> pulls down network sources, has to run on background thread RX stream 2 (transform of stream 1) --> renders geoJson on map, has to run on main thread.

scolsen avatar Sep 23 '21 16:09 scolsen

In theory calling subscribeOn(schedulers.io) and then observeOn(schedulers.ui) should fix the issue--it does workaround the exception, but it doesn't actually make the functionality work, and basemap imagery seems to be completely broken.

scolsen avatar Sep 23 '21 16:09 scolsen

RX stream 1 (offline basemap repo) --> pulls down network sources, has to run on background thread

Is this done when creating the TileProvider, or when rendering?

RX stream 2 (transform of stream 1) --> renders geoJson on map, has to run on main thread.

If the stream is transformed into LiveData it will automatically be observed on the UI thread.

gino-m avatar Sep 23 '21 23:09 gino-m

RX stream 1 (offline basemap repo) --> pulls down network sources, has to run on background thread

The stream originates in the repository and is called on instantiation of the map in the selector UI. We fetch web tiles and render them so the user can see what they are going to download. It's called onMapReady. Once the webtiles are fetched, the list of urls is passed to the map to create tile providers.

If the stream is transformed into LiveData it will automatically be observed on the UI thread.

ah ok, maybe that will work!

scolsen avatar Sep 24 '21 00:09 scolsen

RX stream 1 (offline basemap repo) --> pulls down network sources, has to run on background thread

The stream originates in the repository and is called on instantiation of the map in the selector UI. We fetch web tiles and render them so the user can see what they are going to download. It's called onMapReady. Once the webtiles are fetched, the list of urls is passed to the map to create tile providers.

I see.. I don't understand threading in Maps SDK enough to know what would happen if you ran an async operation in a different thread in onMapReady.. I assume it would just work, but maybe there are some limitations on what can be done there? What happens if you hardcode a a few URLs instead?

If the stream is transformed into LiveData it will automatically be observed on the UI thread.

ah ok, maybe that will work!

Cool, lmk!

gino-m avatar Sep 27 '21 15:09 gino-m

@scolsen For your attention when updating the offline basemaps functionality.

gino-m avatar Feb 27 '23 17:02 gino-m