RedditPP Mascot

RedditPP

r/xamarindevelopers • … • u/NotVeryBad

[Xamarin Forms] Adding markers to map from Firebase subscription

\*edit\* I think I figured this out - it's as simple as wrapping adding the marker to the map in a Device.BeginInvokeOnMainThread call!

(preface) I'm a very novice mobile developer working on a Xamarin Forms app which involves adding markers to a Google map (via the x-platform Xamarin.Forms.GoogleMaps package) that have been added by multiple users. Rather than inelegantly polling the marker backend endpoint for changes, I want to use Firebase Realtime DB to store the marker points, and then use the RTDB streaming functionality to get the newly added markers.

The package that I'm using for Firebase implements the streaming via a Subscription mechanism. I've run into an issue when trying to add markers via the subscription because Android is throwing IllegalStateException - not on the main thread. I'm guessing this is because the subscription is running in its own thread, and Google Maps only allows markers to be added on the main thread.

Being a novice, I'm unsure on how to address this problem. I thought about using MessagingCenter, but the part in the documentation where it says "The delegate that's executed by the Subscribe method will be executed on the same thread that publishes the message using the Send method." leads me to think I'll be no better off this way. Is there any way to run the subscriber on the main thread? Or do I need to use some sort of other Observable data structure?

Thanks in advance!

— 0