I might have to remove Koin from my project as I k...
# koin
k
I might have to remove Koin from my project as I keep getting crashes due to Fragments being reinstantiated. I've put all kinds of code in to check the status and stop/restart koin.
These are crashes that don't happen in testing but out in the field. I have no way to reproduce them
s
stacktrace?
k
Copy code
Exception:     org.koin.core.error.NoBeanDefFoundException
Ex. Message:   No definition found for class:'com.intuit.payments.billpay.paymentscard.PaymentsCardViewModel'. Check your definitions!
Thread:        Thread[main,5,main]
Memory Usage:  22 / 1812 MB (1%)

Original stack trace:
======================
java.lang.RuntimeException : java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:504)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Method.java)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
        Caused by: org.koin.core.error.NoBeanDefFoundException : No definition found for class:'com.intuit.payments.billpay.paymentscard.PaymentsCardViewModel'. Check your definitions!
        at org.koin.core.scope.Scope.a(SourceFile:15)
        at org.koin.core.scope.Scope.get(SourceFile:7)
        at org.koin.androidx.viewmodel.factory.DefaultViewModelFactory.create(SourceFile:1)
        at androidx.lifecycle.ViewModelProvider.get(SourceFile:11)
        at androidx.lifecycle.ViewModelProvider.get(SourceFile:2)
        at org.koin.androidx.viewmodel.ViewModelResolverKt.get(SourceFile:3)
        at org.koin.androidx.viewmodel.ViewModelResolverKt.resolveInstance(SourceFile:2)
        at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(SourceFile:10)
        at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(SourceFile:8)
        at org.koin.androidx.viewmodel.koin.KoinExtKt.getViewModel(SourceFile:2)
        at org.koin.androidx.viewmodel.ext.android.FragmentExtKt.getViewModel(SourceFile:2)
        at com.intuit.payments.billpay.paymentscard.PaymentsCardWidget$$special$$inlined$viewModel$2.invoke(SourceFile:2)
        at com.intuit.payments.billpay.paymentscard.PaymentsCardWidget$$special$$inlined$viewModel$2.invoke(Unknown Source)
        at kotlin.UnsafeLazyImpl.getValue(SourceFile:2)
        at com.intuit.payments.billpay.paymentscard.PaymentsCardWidget.b(Unknown Source)
        at com.intuit.payments.billpay.paymentscard.PaymentsCardWidget.onViewCreated(SourceFile:5)
        at androidx.fragment.app.Fragment.performViewCreated(SourceFile:1)
        at androidx.fragment.app.FragmentStateManager.createView(SourceFile:27)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(SourceFile:28)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(SourceFile:73)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(SourceFile:10)
        at androidx.fragment.app.FragmentManager.execPendingActions(SourceFile:14)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(SourceFile:11)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(SourceFile:6)
        at androidx.fragment.app.FragmentActivity.onStart(SourceFile:5)
        at androidx.appcompat.app.AppCompatActivity.onStart(SourceFile:1)
It seems like the Fragment gets removed, then a onViewCreated is called. I shut down Koin in onViewDestroyed and then recreate it all in onViewCreated.
This is a library
s
I've never shut down all of koin onViewDetroyed and recreated unless i was restarting the entire app in some other type of mode. I do this when i target internal API environments and need dependencies to reinitialize e.g. Flipper / Stetho
What's the reason you're shutting down Koin?
k
So, my current theory is that Koin has all of it's objects tied to some global state and for some reason when the Fragment is redisplayed, it is tied to older code. I'm not using startKoin but loadModules
👍 1
s
It seems like the Fragment gets removed, then a onViewCreated is called. I shut down Koin in onViewDestroyed and then recreate it all in onViewCreated.
yeah seems to me your modules are unloaded at some point when they should not be / are required by your view. Im not sure your use case but maybe see if being less aggressive about loading / unloading modules fixes your problem?
k
I've had to get more aggressive since the crashes continue. It started not even unloading
s
i see
Some more info that would be nice for debugging: 1. your module definition 2. where you load / unload your modules 3. where you inject your ViewModel
k
I use a function that returns a
Copy code
koinApplication
This function loads the modules. One of the modules has a viewModel {} method
a
how do you use Koin, with not using regular startKoin?
what is your strategy about using loadKoinModules?
k
Since this is a library, I can't use startKoin. Using koinApplication seems to work well (except for the crashes)
a
then do you use loadKoinModules from your Koin instance ?
as you are isolating a Koin instance, you can’t use directly the default
loadKoinModules
function that is using the default Koin instance. You need to deal with your Koin instance
k
Inside of the KoinApplication I use the modules call to load my modules
I then have a global Koin reference with my classes implementing a KoinComponent that returns that Koin instance
a
Inside of the KoinApplication I use the modules call to load my modules
still have issues then? 🤔
k
Yes. This hasn’t been fixed yet.
a
can you send a Github sample to help fix that?
k
I can’t send my work files. I could probably send a few pieces of code that shows what I am doing
a
ok, let’s try with that. Not necessary your work files, but a very small sample
k
I've tried to put in just the code that it DI related
I'll probably just need to remove Koin and use Dagger or my own solution
a
Koin should be isolated. It’s perhaps a regression in the version you have 🤔
what version do you use?
are you using ViewModel. Seems that last ViewModel API doesn’t fallback on Isolated context if a component is tagged KoinComponent
k
I'm using 2.2.2
509 Views