Hello guys I have a question about `scope` and `v...
# koin
q
Hello guys I have a question about
scope
and
view models
I found https://github.com/InsertKoinIO/koin/issues/440#issuecomment-489583308 this solution but still have questions. Could you please hellp me. I see that recommended way is to use.
Copy code
private val viewModel: TabViewModel by viewModel { parametersOf(lifecycleScope.id) }
but what if I need global VM in many fragments with different
lifecycle
What is best practice for this case. how I should create scope and that it between fragments? UPD: googled laso
sharedViewModel
but it shared VM based on Actiity. and I need to share based on main fragment.
a
Usually you create VM inside the Activity and all the fragments just call
sharedViewModel
instance. I also prefer to attach VM scope to activity.
q
But what if I need scope less than Activity life. For example it’s fragment + ViewPager with different fragments. and life should depends on this TabFragment life
I see for the
sharedViewModel
activity is
Copy code
owner: ViewModelStoreOwner
a
well, you can do same with parent fragment. You will create an instance inside parent fragment and all the child fragments will call for shared instance
q
How exactly I should create it? as I see it takes instance based on actiity or scope that eas provided If I share test project could you please help me?
a
Sure
q
I really appreciate it. Here is the test project https://github.com/YvgenTroshchiy/TestKoinScope
For now I see and option like this:
Copy code
val myScopeInstance = val myScopeInstance = koin.createScope("myScopeId",named(¨MY_SCOPE¨)).createScope("myScopeId",named(¨MY_SCOPE¨))
have and instance of koin in the Application and then ask for scope that I need from this variable
a
can you please share what you want to see.. i don’t get it… while tab fragment is removed you want to clear the instance of VM?
q
Exactly!
a
I could imagine it like this your module declare scope for
TabFragment
Copy code
val viewModelModule = module {
    scope(named<TabFragment>()) {
        viewModel { TabViewModel() }
    }
}
TabFragment will own VM like
Copy code
private val viewModel: TabViewModel by lifecycleScope.viewModel(this)
And child fragments will get it like this
Copy code
private lateinit var viewModel: TabViewModel

    override fun onAttach(context: Context) {
        super.onAttach(context)
        viewModel = requireParentFragment().lifecycleScope.getViewModel(requireParentFragment())
    }
As you can see below… each time while TabFragment is created, new instance of
TabViewModel
is created and passed to child fragments
Copy code
viewModel in TabFragment: com.troshchiy.testkoinscope.TabViewModel@dee6937
TabFragment. 169590436
viewModel in Fragment 1: com.troshchiy.testkoinscope.TabViewModel@dee6937
Fragment1. 245425006
viewModel in Fragment 2: com.troshchiy.testkoinscope.TabViewModel@dee6937
Fragment2. 142329978
ViewModel. init: 228
viewModel in TabFragment: com.troshchiy.testkoinscope.TabViewModel@cc6fdfd
TabFragment. 13331186
viewModel in Fragment 1: com.troshchiy.testkoinscope.TabViewModel@cc6fdfd
Fragment1. 115849630
viewModel in Fragment 2: com.troshchiy.testkoinscope.TabViewModel@cc6fdfd
Hope this helps 😄
q
@aipok Thank you a lot for help . this main “trick”
Copy code
viewModel = requireParentFragment().lifecycleScope.getViewModel(requireParentFragment())
works great!
🖖 1