简体   繁体   English

为什么我可以使用实例创建 ViewModel

[英]Why can i create ViewModel with instance

I create ViewModel with ViewModelProvider.Factory in my fragment.我在片段中使用 ViewModelProvider.Factory 创建 ViewModel。 Everthing work fine.一切正常。

var reportViewModel = ViewModelProviders.of(this,ViewModelProvide(RemoteDataProviderImpl(
        WeakReference(context!!))))
       .get(ReportViewModel::class.java)

But after i create viewmodel directly with instance and then work everthing fine但是在我直接使用实例创建视图模型然后一切正常之后

var reportViewModel = ReportViewModel(RemoteDataProviderImpl(WeakReference(context!!)))

It is simple ViewModel它是简单的 ViewModel

class ReportViewModel(private var provider:RemoteDataProvider) : ViewModel(){
var posts = MutableLiveData<List<Report>>()
fun getPost(){
    provider.getComments().enqueue(object : Callback<ArrayList<Report>> {
        override fun onFailure(call: Call<ArrayList<Report>>, t: Throwable) {
            t.message
        }
        override fun onResponse(
            call: Call<ArrayList<Report>>,
            response: Response<ArrayList<Report>>
        ) {
            if (response.isSuccessful){
                val reports = response.body()
                posts.value = reports
                }
            }

        })
    }
}

What is difference this two code.这两个代码有什么区别。 If i can create viewModel with directly why i need ViewModelProviders?如果我可以直接创建 viewModel 为什么我需要 ViewModelProviders?

Using the ViewModelProviders will allow Android to attach the ViewModel to the internal ViewModelStore .使用ViewModelProviders将允许 Android 将ViewModel附加到内部ViewModelStore This will allow few niceties, like getting the onCleared function called when the activity or the fragment are destroyed.这将允许一些细节,例如在活动或片段被销毁时调用onCleared function。 Another one is the viewmodel's retantion upon configuration changes (eg. orientation).另一个是视图模型对配置更改(例如方向)的限制。

 ReportViewModel(RemoteDataProviderImpl(WeakReference(context!!)))

will将要

  • create a new reference of the viewmodel without any scope of the activity or fragment.创建没有任何活动或片段的 scope的视图模型的新引用。

  • A new reference of the viewmodel will be created in onCreate of the activity. viewmodelnew reference将在活动的onCreate中创建。 ( even if the activity is rotated or configuration is changed ) 即使活动旋转或配置更改

  • It would not observe the scope of the activity,ie, it would exist even when the activity is destroyed !不会观察到活动的scope,即即使活动被destroyed它也会存在

While尽管

 ViewModelProvider.of(this).get(...)

will将要

  • Observe the scope of the activity, ie, when the activity is finished , onCleared(..) of the viewmodel would be called ! Observe activity的scope,即当activity is finished时,会调用viewmodel的onCleared(..) ( Viewmodel would be destroyed ) 视图模型将被破坏

  • Same instance of the viewmodel would persist even during the configuration change .即使在configuration change期间, Same instance of the viewmodel也会persist

Because when you change config(rotate the phone), if you create viewModel with directly, the viewModel will destroy and lose all the data in viewModel.因为当你更改配置(旋转手机)时,如果你直接创建viewModel,viewModel会破坏并丢失viewModel中的所有数据。

But if you use ViewModelProvider to create viewModel, ViewModelProvider will save the viewModel instance and data, so you won't lose any data when config change.但是如果你使用ViewModelProvider来创建 viewModel, ViewModelProvider会保存 viewModel 实例和数据,所以当配置改变时你不会丢失任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM