Skip to content

App Crashing when to use obserserver two times #7

@burhankhanzada

Description

@burhankhanzada

Describe the bug
My app is based on single architecture activity so there for only one activity and many fragments so I initialized orb instance in activity a store its reference to ViewModel to use it my all fragments but app gettings crash when I try to add another observer see log cat

Process: com.example.app, PID: 2606
    android.net.ConnectivityManager$TooManyRequestsException
        at android.net.ConnectivityManager.convertServiceException(ConnectivityManager.java:3378)
        at android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:3564)
        at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:3885)
        at android.net.ConnectivityManager.registerNetworkCallback(ConnectivityManager.java:3866)
        at com.ezralazuardy.orb.OrbEngine.registerNetworkCallback(OrbEngine.kt:125)
        at com.ezralazuardy.orb.OrbEngine.onActive(OrbEngine.kt:62)
        at androidx.lifecycle.LiveData.changeActiveCounter(LiveData.java:390)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:466)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:425)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
        at androidx.lifecycle.LiveData.observe(LiveData.java:205)
        at com.ezralazuardy.orb.Orb.observe(Orb.kt:87)
        at com.example.app.fragments.ViewPagerFragment.showNoBundlesNoInternet(ViewPagerFragment.kt:332)

To Reproduce
Steps to reproduce the behavior:
1.Make orb varaible in ViewModel.kt to use it on fragments

class MainViewModel : ViewModel() {
    lateinit var orb: Orb
    var networkState: Boolean = false
}

2.In MainActivity.kt

class MainActivity : AppCompatActivity(R.layout.main_activity) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mainViewModel: MainViewModel by viewModels()
        mainViewModel.orb = Orb.with(this)
        mainViewModel.orb.observe {
            mainViewModel.networkState = it.connected
        }
    }
}
  1. Use in ViewPagerFragment
class ViewPagerFragment : Fragment() {
    private val mainViewModel: MainViewModel by activityViewModels()
    private val orb by lazy { mainViewModel.orb }

    //...

private fun showNoBundlesNoInternet() {
        if (mainViewModel.networkState) showNoBundles()
        else {
            showNoInternet()
            mainViewModel.orb.observe {
                if (it.connected) {
                    //...
                } else {
                    //...
                }
            }
        }
    }

Expected behavior
The expected behavior is adding more than one observer should not crash the app

Metadata

Metadata

Assignees

Labels

errorThere is an error in the project

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions