This is a continuation of a research spawned by the answer to my older, broader question.
Is it correct to use a single Cursor (obtained via CursorLoader) to feed two distinct views, namely:
- ListView via SimpleCursorAdapter
- com.google.android.gms.maps.GoogleMap with Markers?
Results i'm having so far:
- If both views are given the cursor synchronously (i.e.: no other threads involved), than visually all works fine, except that UI is blocked considerably when populating GoogleMap
- If GoogleMap is being populated from Cursor in AsyncTask, then UI becomes responsive, but there're anomalies with the ListView: some rows are duplicated, also some markers are not getting to the map.
Details of my setup
- SQLite Cursor is loaded in FragmentActivity during onResume()
- The above activity hosts ViewPager with two fragments: ListFragment and SupportMapFragment (The consequence of using ViewPager is that both fragments will always be active)
- Cursor from activity is delivered to fragments via Otto bus (for unfamiliar: this is merely a thin replacement for callback interfaces, that delivers results synchronously on the main thread)
I believe this is correct to do this. What could be improved though is that, if MapFragment is not visible it does not populate the Map. It populates it when user swipes to the MapFragment page.
Alternatively, you could even do following. When MapFragment is invisible it starts populating the map, but it does this in chunks (e.g. by 5 markers in a chunk). Once one chunk is populated, you can start with the next chunk by scheduling another runnable for been executed in main thread handler. This will not block main thread and will prepare the map "in background".