将SharedFragment与`by-activityViewModels'一起使用会导致碎片访问缓慢



因此,在我之前的另一个问题中,有人告诉我要使用共享视图模型(如何存储片段中的数据,以便在MVVM架构中重用?(

下面的代码没有使用共享视图模型,而且更快

package io.github.andraantariksa.cratesio.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import io.github.andraantariksa.cratesio.R
import io.github.andraantariksa.cratesio.data.api.model.CrateSummary.CrateSummary
import io.github.andraantariksa.cratesio.utils.InjectorUtils
import kotlinx.android.synthetic.main.fragment_summary.*
import kotlinx.coroutines.launch

class SummaryFragment : ScopedFragment() {
private lateinit var viewModelFactory: CratesViewModelFactory
private lateinit var viewModel: CratesViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_summary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModelFactory = InjectorUtils.provideCratesViewModelFactory(context!!)
viewModel = ViewModelProvider(this, viewModelFactory)
.get(CratesViewModel::class.java)
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it)
}
fetchRecyclerViewData()
}
private fun fetchRecyclerViewData() {
launch {
val crateSummaryLiveData = viewModel.crateSummary.await()
crateSummaryLiveData.observe(viewLifecycleOwner, Observer {
setupSummaryRecyclerView(it)
})
}
}
private fun setupSummaryRecyclerView(crateSummary: CrateSummary) {
viewModel.cratesSummaryLast = crateSummary
textViewCratesTotalNum.text = crateSummary.numCrates.toString()
textViewDownloadsTotalNum.text = crateSummary.numDownloads.toString()
val newCratesAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.newCrates)
recyclerViewNewCrates.layoutManager = LinearLayoutManager(context)
recyclerViewNewCrates.adapter = newCratesAdapter
val mostDownloadedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.mostDownloaded)
recyclerViewMostDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostDownloaded.adapter = mostDownloadedAdapter
val justUpdatedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.justUpdated)
recyclerViewJustUpdated.layoutManager = LinearLayoutManager(context)
recyclerViewJustUpdated.adapter = justUpdatedAdapter
val mostRecentlyDownloaded =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewMostRecentlyDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostRecentlyDownloaded.adapter = mostRecentlyDownloaded
val mostPopularKeywords =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewPopularKeywords.layoutManager = LinearLayoutManager(context)
recyclerViewPopularKeywords.adapter = mostPopularKeywords
val popularCategories =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerPopularCategories.layoutManager = LinearLayoutManager(context)
recyclerPopularCategories.adapter = popularCategories
}
}

但是,通过使用共享视图模型,访问/打开片段变得更慢

package io.github.andraantariksa.cratesio.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import io.github.andraantariksa.cratesio.R
import io.github.andraantariksa.cratesio.data.api.model.CrateSummary.CrateSummary
import io.github.andraantariksa.cratesio.utils.InjectorUtils
import kotlinx.android.synthetic.main.fragment_summary.*
import kotlinx.coroutines.launch

class SummaryFragment : ScopedFragment() {
//    private lateinit var viewModelFactory: CratesViewModelFactory
//    private lateinit var viewModel: CratesViewModel
private val viewModel: CratesViewModel by activityViewModels {
InjectorUtils.provideCratesViewModelFactory(requireContext())
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_summary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
//        viewModelFactory = InjectorUtils.provideCratesViewModelFactory(context!!)
//        viewModel = ViewModelProvider(this, viewModelFactory)
//            .get(CratesViewModel::class.java)
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it)
}
fetchRecyclerViewData()
}
private fun fetchRecyclerViewData() {
launch {
val crateSummaryLiveData = viewModel.crateSummary.await()
crateSummaryLiveData.observe(viewLifecycleOwner, Observer {
setupSummaryRecyclerView(it)
})
}
}
private fun setupSummaryRecyclerView(crateSummary: CrateSummary) {
viewModel.cratesSummaryLast = crateSummary
textViewCratesTotalNum.text = crateSummary.numCrates.toString()
textViewDownloadsTotalNum.text = crateSummary.numDownloads.toString()
val newCratesAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.newCrates)
recyclerViewNewCrates.layoutManager = LinearLayoutManager(context)
recyclerViewNewCrates.adapter = newCratesAdapter
val mostDownloadedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.mostDownloaded)
recyclerViewMostDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostDownloaded.adapter = mostDownloadedAdapter
val justUpdatedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.justUpdated)
recyclerViewJustUpdated.layoutManager = LinearLayoutManager(context)
recyclerViewJustUpdated.adapter = justUpdatedAdapter
val mostRecentlyDownloaded =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewMostRecentlyDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostRecentlyDownloaded.adapter = mostRecentlyDownloaded
val mostPopularKeywords =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewPopularKeywords.layoutManager = LinearLayoutManager(context)
recyclerViewPopularKeywords.adapter = mostPopularKeywords
val popularCategories =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerPopularCategories.layoutManager = LinearLayoutManager(context)
recyclerPopularCategories.adapter = popularCategories
}
}

那么,是什么导致碎片打开缓慢呢?我该如何修复它?我使用共享视图模型的方法是否错误?

编辑

所以我必须以异步方式运行setupSummaryRecyclerView。但仍然较慢

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModelFactory = InjectorUtils.provideCratesViewModelFactory(context!!)
viewModel = ViewModelProvider(this, viewModelFactory)
.get(CratesViewModel::class.java)
launch {
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it)
}
fetchRecyclerViewData()
}
}

事实证明,我忘记了以异步方式setupSummaryRecyclerView:(。另外,by activityViewModels看起来很慢,所以我也让它异步运行。

package io.github.andraantariksa.cratesio.ui
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import io.github.andraantariksa.cratesio.R
import io.github.andraantariksa.cratesio.data.api.model.CrateSummary.CrateSummary
import io.github.andraantariksa.cratesio.utils.InjectorUtils
import kotlinx.android.synthetic.main.fragment_summary.*
import kotlinx.coroutines.launch

class SummaryFragment : ScopedFragment() {
private lateinit var viewModel: CratesViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_summary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
launch {
val viewModelLateInit by activityViewModels<CratesViewModel> {
InjectorUtils.provideCratesViewModelFactory(requireContext())
}
viewModel = viewModelLateInit
fetchRecyclerViewData()
}
}
private fun fetchRecyclerViewData() {
launch {
viewModel.cratesSummaryLast?.let {
setupSummaryRecyclerView(it, false)
}
val crateSummaryLiveData = viewModel.crateSummary.await()
crateSummaryLiveData.observe(viewLifecycleOwner, Observer {
setupSummaryRecyclerView(it)
})
}
}
private fun setupSummaryRecyclerView(crateSummary: CrateSummary, cache: Boolean = true) {
if (cache) {
viewModel.cratesSummaryLast = crateSummary
}
textViewCratesTotalNum.text = crateSummary.numCrates.toString()
textViewDownloadsTotalNum.text = crateSummary.numDownloads.toString()
val newCratesAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.newCrates)
recyclerViewNewCrates.layoutManager = LinearLayoutManager(context)
recyclerViewNewCrates.adapter = newCratesAdapter
val mostDownloadedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.mostDownloaded)
recyclerViewMostDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostDownloaded.adapter = mostDownloadedAdapter
val justUpdatedAdapter =
CrateSummaryRecyclerViewAdapter(crateSummary.justUpdated)
recyclerViewJustUpdated.layoutManager = LinearLayoutManager(context)
recyclerViewJustUpdated.adapter = justUpdatedAdapter
val mostRecentlyDownloaded =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewMostRecentlyDownloaded.layoutManager = LinearLayoutManager(context)
recyclerViewMostRecentlyDownloaded.adapter = mostRecentlyDownloaded
val mostPopularKeywords =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerViewPopularKeywords.layoutManager = LinearLayoutManager(context)
recyclerViewPopularKeywords.adapter = mostPopularKeywords
val popularCategories =
CrateSummaryRecyclerViewAdapter(crateSummary.mostRecentlyDownloaded)
recyclerPopularCategories.layoutManager = LinearLayoutManager(context)
recyclerPopularCategories.adapter = popularCategories
}
}

相关内容

  • 没有找到相关文章

最新更新