In my android project I have a google map with markers that should update live. I decided to implement callbacks as a way to make sure I have the data before I draw the markers. However when Firebase Realtime Database gets updated when I am on the map my app crashes. I believe this is due to calling a callback even though I didn't call the function
MapsFragment
private fun loadMarkers() {
viewModel.loadAllEvents(0){
//Load markers
}
ViewModel
fun loadAllEvents(num : Int, callback: (MutableList<LocationEvent>) -> Unit) {
_listOfEvents.value!!.clear()
val postListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
//Logic and creates list to return
callback(listOfEvents.value!!)
}
override fun onCancelled(databaseError: DatabaseError) {
Log.w("MainFragment", "loadPost:onCancelled", databaseError.toException())
}
}
locationDatabase.addValueEventListener(postListener)
}
My theory is that since the View Model function is called when data changes and not from MapsFragment function, calling callback causes my app to crash.
Question
Is there a correct way to implement this feature and if so could you please provide an example? If this question has been answered please point me to the right direction.
Error Msg
E/SystemServiceRegistry: No service published for: payjoy_access_service
android.os.ServiceManager$ServiceNotFoundException: No service published for: payjoy_access_service
at android.os.ServiceManager.getServiceOrThrow(ServiceManager.java:153)
at android.app.SystemServiceRegistry$126.createService(SystemServiceRegistry.java:1694)
at android.app.SystemServiceRegistry$StaticServiceFetcher.getService(SystemServiceRegistry.java:1919)
at android.app.SystemServiceRegistry.getSystemService(SystemServiceRegistry.java:1517)
at android.app.ContextImpl.getSystemService(ContextImpl.java:1938)
at com.android.server.wm.ActivityStack.shouldSendPayJoyBroadcast(ActivityStack.java:3644)
at com.android.server.wm.ActivityStack.access$000(ActivityStack.java:188)
at com.android.server.wm.ActivityStack$ActivityStackHandler.handleMessage(ActivityStack.java:316)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:250)
at android.os.HandlerThread.run(HandlerThread.java:67)
at com.android.server.ServiceThread.run(ServiceThread.java:44)