toothpick icon indicating copy to clipboard operation
toothpick copied to clipboard

Dsl to close scope when ViewModel is cleared

Open afaucogney opened this issue 4 years ago • 4 comments

Hello, I'm trying to handle the new extension function provided by TP3.

// Close Scope when the Activity/Fragment is destroyed
KTP.openScopes(this)
        .closeOnDestroy(this)
        .inject(this)

// Close Scope when the ViewModel attached to the Activity/Fragment is cleared
KTP.openScopes(this)
        .closeOnViewModelCleared(this)
        .inject(this)

// Create binding on the scope to be able to inject the ViewModel
KTP.openScopes(<name>)
        .installViewModelBinding(<BackpackViewModel>this)
        .inject(this)

1/ I wonder why you didn't provided a closeOnCleared() to be used inside the VM ?

At a first look, I thought it will be closeOnViewModelCleared but this one is for the fragment. From my understanding, this will be only usable for advanced use cases, isn't it ? Because if you defined a subScope FRAGMENT from VIEMMODEL, if the Vm scope is close, it subScope (the one for the FRAGMENT) too, isn't it ? So what . is the sens of this ?

2/ So please try to give us an example about how to use this in a real case.

3/ By my side, since I use TP, I used it with VM, and I do define 2 independant scopes

app -> activityVm app -> activity

or

app -> fragmentVm app -> activity -> fragment

but no app -> vm -> fragment

I know it is a kind of implicit scope tree part, because vm is linked to the fragment thanks to Android Sdk...

For my perspective, I do not want any direct relation of views (activity or fragment) in my vm. So I would not like to have a the view scope as a subscope of the vm ! Also, I inject the VM inside the activity/fragment, so I'm not sure how (or even if this is possible) to create a scope with an injected instance !

I thinks this is what you do here : https://github.com/stephanenicolas/toothpick/blob/master/toothpick-sample/src/main/java/com/example/toothpick/activity/AdvancedBackpackItemsActivity.kt

But what is the real intention of this ? Am I doing the wrong way ?

afaucogney avatar Sep 27 '19 09:09 afaucogney

Hi @afaucogney , sorry for the late reply.

You mean that closeOnViewModelCleared does not work for a ViewModel attached to an activity?

dlemures avatar Oct 18 '19 17:10 dlemures

@dlemures no, I mean one function is missing to close the ViewModel scope, when the viewmodel is cleared.

Inside the viewmodel, like this for now:

abstract class KtpBaseViewModel(app: Application) : ViewModel(app) {

    private val compositeDisposable = CompositeDisposable()

    open fun onCreated() {
        KTP.openRootScope()
            .openSubScope(this)
            .inject(this)
    }

    override fun onCleared() {
        compositeDisposable.clear()
        KTP.closeScope(this)
        super.onCleared()
    }
}

Did I missunderstand something ?

afaucogney avatar Oct 18 '19 19:10 afaucogney

@dlemures I just fix the question, with extra info ! Maybe this will be 2 issue at the end.

afaucogney avatar Oct 18 '19 20:10 afaucogney

Ok, so:

  1. Provide a method to close VM scope when VM is destroyed. But this case using the VM instance and not the activity/fragment.
  2. Show a example of closeOnViewModelCleared and why it is useful?

dlemures avatar Oct 19 '19 21:10 dlemures