question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parameters in by inject() are not lazy

See original GitHub issue

After playing around a little bit with the new parameters i came accross another issue.

class MyFragment : Fragment() {
   val presenter: MyPresenter by inject(parameters = mapOf("activity" to activity!!)
   // or since support lib 27.1.0 we can call
   val presenter: MyPresenter by inject(parameters = mapOf("activity" to requiredActivity())
}

if you instantiate this fragment the presenter gets created instantly and not on his first call. this leads to the following exception

Caused by: java.lang.IllegalStateException: Fragment MyFragment{3833b34} not attached to an activity.

currently a workaround is to inject the presenter like this

class MyFragment : Fragment() {
   val presenter: MyPresenter by lazy { (StandAloneContext.koinContext as KoinContext).get<MyPresenter>("",  mapOf("activity" to activity!!)) }
}

this works and the presenter gets only created after the first call

i think it would be great if by inject() has the same functionality as by lazy or if you will not change this direct instantiation you could provide another by injectLazy() function or so…

I am looking forward for your feedback

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
arnaudgiulianicommented, Mar 6, 2018

Ok. Fixed.

I migrate all params to be functional. Koin has now a very nice syntax to supply dynamic params (lambda directly opened for parameters):

val presenter: MyPresenter by inject { mapOf("key" to myKey) }

Just migrate my MVP sample: https://github.com/Ekito/koin-samples/blob/0.9.1/samples/android-weatherapp/app/src/main/kotlin/org/koin/sampleapp/view/search/SearchActivity.kt#L23

You can play with Koin 0.9.1-alpha-5 👍

0reactions
fredy-mederoscommented, Mar 5, 2018

The same with viewModel()… I try with @Bodo1981 solution and it works

inline fun <reified T : ViewModel> LifecycleOwner.viewModel(
    fromActivity: Boolean = true,
    key: String? = null,
    name: String? = null,
    noinline paramsFunc: () -> ParameterMap = { emptyMap() }
): Lazy<T> {
    return viewModelByClass(fromActivity, T::class, key, name, paramsFunc)
}

fun <T : ViewModel> LifecycleOwner.viewModelByClass(fromActivity: Boolean = true, clazz: KClass<T>, key: String? = null, name: String? = null, paramsFunc: () -> ParameterMap = { emptyMap() }): Lazy<T> {
    return lazy { getViewModelByClass(fromActivity, clazz, key, name, paramsFunc()) }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Lazy Evaluation To Prevent Parameter Drilling in ...
While I do think injection does solve the problem of decoupling, it can face a huge flaw when you have tall class relation...
Read more >
Dependency Injection using Koin - Medium
Dependency Inversion: Upper level modules should not depend on lower level ... by inject() - lazy evaluated instance from Koin container.
Read more >
Lazy Injection Example — injectable 3.4.7 documentation
In our LazyInjection example class we inject the ServiceA lazily by specifying the parameter lazy=True to Autowired and we also inject the ...
Read more >
Android Koin injected viewmodel with multiple same class ...
params.get() resolves the parameters by type. Since both are strings, it will match the first one in both cases. It works implicitly only...
Read more >
Lazy Dependency Injection for .NET - DEV Community ‍ ‍
The DoWork method uses either ServiceA or ServiceB depending on the value parameter. The important thing is that the method does not ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found