Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage User Scope using Koin?

I'm trying to create a user scope using Koin. When the user is logged, I'm creating the scope :

val scope = getKoin().createScope("USER_SCOPE")

And when the user clicks on logout, I'm destroying the scope

    scope?.let {userScope ->
        userScope.close()
        getKoin().deleteScope(userScope.id)
    }

In my koin module, I have a scoped UserRepository which should live only during the user session. I also have ViewModels and Use Cases which are using this repository, and I try to inject the scoped repo inside them

val appModule = module {
    scoped<UserRepository> { UserDataRepository() }
    viewModel { UserViewModel(getScope("USER_SCOPE").get()) }
    factory { MyUseCase(getScope("USER_SCOPE").get()) }
}

On the first login, it is working normally, I have my user repo injected in my viewmodel and use case. But after a logout (which is deleting the scope) and after another login, the UserRepository instance is still exactly the same.

Do I miss something in the scope usage ?

like image 878
Mathieu H. Avatar asked May 10 '19 15:05

Mathieu H.


People also ask

How do you use a KOIN scope?

Type Qualified ScopeTo have the Container as a type scope, we need to subtype it to KoinScopeComponent . Then we can set up the module as below. So to create the scope, we first need to get the Container object first. To get the dependencies is the same as stringQualifiedScope above.

What is a scope in Android?

In a nutshell, a scope is a space where we are maintaining instances for a specific duration of time. What about Android scope? We maintain instances for the duration of an Android component lifecycle. Android components have their own lifecycle (Activity, Fragment, Service …).

What is KOIN single?

Understanding Terminologies in Koin single - it creates a singleton that can be used across the app as a singular instance. factory - it provides a bean definition, which will create a new instance each time it is injected. get() - it is used in the constructor of a class to provide the required dependency.

What is KOIN Kotlin?

Koin is a smart Kotlin dependency injection library to keep you focused on your app, not on your tools. Koin gives you simple tools and API to let you build, assemble Kotlin related technologies into your application and let you scale your business with easyness. Define Modules. Start Koin. Start on Android.


1 Answers

Migrating from koin 2.0.0-rc-2 to koin 2.0.0-GA solved my problem.

After migrating, it is not possible to declare a scoped instance outside a scope. So I adapted my appmodule this way :

   val appModule = module {
        scope(named("USER_SCOPE")) {
            scoped<UserRepository> { UserDataRepository() }
        }
        viewModel { UserViewModel(getScope("USER_SCOPE").get()) }
        factory { MyUseCase(getScope("USER_SCOPE").get()) }
    }

Scope declaration is also a bit different :

val scope = getKoin().createScope("USER_SCOPE", named("USER_SCOPE"))

This way I have my UserRepository recreated after a logout/login.

like image 54
Mathieu H. Avatar answered Sep 29 '22 00:09

Mathieu H.