AutoClearedValue accessed from another thread after View is Destroyed

Viewed 473

I am using AutoClearedValue class from this link and when view is destroyed, backing field becomes null and that is good but i have a thread(actually a kotlin coroutine) that after it is done, it accesses the value(which uses autoCleared) but if before it's Job is done i navigate to another fragment(view of this fragment is destroyed), then it tries to access the value, but since it is null i get an exception and therefore a crash. what can i do about this?

also for which variables this autoCleared needs to be used? i use it for viewBinding and recyclerview adapters.

1 Answers

You have 2 option:

1- Cancelling all the running job(s) that may access to view after its destruction. override onDestroyView() to do it.

Also, you can launch the coroutines viewLifecycleOwner.lifecycleScope to canceling it self when view destroy.

viewLifecycleOwner.lifecycleScope.launch {
    // do sth with view    
}

2- (Preferred solution) Use Lifecycle aware components (e.g LiveData) between coroutines and view:

coroutines push the state or data in the live-data and you must observe it with viewLifeCycleOwner scope to update the view.

private val stateLiveData = MutableLiveData<String>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    stateLiveData.observe(viewLifecycleOwner) { value ->
        binding.textView.text = value
    }
}

private fun fetchSomething() {
    lifecycleScope.launch {
        delay(10_000)
        stateLiveData.value = "Hello"
    }
}
Related