ViewModel初始化时出现StackOverflowError

时间:2018-10-19 15:29:08

标签: android kotlin viewmodel

在ViewModel类初始化期间遇到了一个奇怪的问题。我想代码是解释问题的最好方法。

此后,我的所有ViewModel都将继承:

abstract class BaseViewModel : ViewModel() {
    internal var args: Bundle? = null
}

我有委托为片段提供ViewModel,它会自动从片段中加载参数。

interface ViewModelFactoryProvider<VIEW_MODEL : BaseViewModel, BINDING : ViewDataBinding> : ReadOnlyProperty<ViewModelFragment<VIEW_MODEL, BINDING>, VIEW_MODEL> {

val viewModelFactory: ViewModelProvider.Factory

override fun getValue(thisRef: ViewModelFragment<VIEW_MODEL, BINDING>, property: KProperty<*>): VIEW_MODEL {
    return ViewModelProviders.of(thisRef, viewModelFactory).get(thisRef.viewModelClass).apply {
        thisRef.arguments?.let { thisRef.viewModel.args = it }
    }
  }
}

上面的基本片段实现接口并以这种方式创建视图模型:

class ViewModelFragment<VIEW_MODEL : BaseViewModel, BINDING : ViewDataBinding> : Fragment(), ViewModelFactoryProvider<VIEW_MODEL, BINDING>{

    @Inject
    override lateinit var viewModelFactory: ViewModelProvider.Factory

    val viewModel: VIEW_MODEL by this
}

一切正常,直到ViewModel的片段没有任何其他参数,否则我得到:

 java.lang.StackOverflowError: stack size 8MB
    at android.support.v4.app.Fragment.getContext(Fragment.java:683)
    at android.support.v4.app.Fragment.getViewModelStore(Fragment.java:327)
    at android.arch.lifecycle.ViewModelStores.of(ViewModelStores.java:60)
    at android.arch.lifecycle.ViewModelProviders.of(ViewModelProviders.java:104)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:15)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$DefaultImpls.getValue(ViewModelFactoryProvider.kt:16)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getValue(ViewModelFragment.kt:20)
    at pl.kulbaka.karol.unilang.tools.ViewModelFragment.getViewModel(ViewModelFragment.kt)
    at pl.kulbaka.karol.unilang.tools.viewmodel.ViewModelFactoryProvider$

目前,我不知道在哪里可以找到圆依存关系,这是什么原因。请帮助。

1 个答案:

答案 0 :(得分:1)

这是由于在代理中计算其值时访问thisRef.viewModel

您通过viewModel委派by this属性。即方法getViewModel()使用委托人的override fun getValue(..)方法,该方法又在getViewModel()中调用thisRef.viewModel.args = it。圆在那点重新开始。

您想要的大致是

override fun getValue(thisRef: ViewModelFragment<VIEW_MODEL, BINDING>, property: KProperty<*>): VIEW_MODEL {
    return ViewModelProviders.of(thisRef, viewModelFactory).get(thisRef.viewModelClass).apply {
        thisRef.arguments?.let { this.args = it }
    }
  }
}

不是在尝试创建的属性上而是在从ViewModel返回的ViewModelProviders.of(...).get(...)上设置参数