0

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)
Myw1005
  • 3
  • 2
  • "my app crashes" When an app crashes, it writes an error message and stack trace to its logcat. Please find those, and add them to your question by clicking the `edit` link under it. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Frank van Puffelen Sep 09 '22 at 23:34
  • @FrankvanPuffelen I have added my error – Myw1005 Sep 10 '22 at 00:06

0 Answers0