[英]I get multiple response from retrofit using MVVM - kotlin
I just started learning MVVM and I am unable to find out why I am getting response from retrofit multiple times.我刚开始学习 MVVM,我无法找出为什么我多次从改造中得到响应。 I am following the videos on how to implement MVVM in kotlin and I would be very glad if you suggested better approach to this.
我正在关注有关如何在 kotlin 中实现 MVVM 的视频,如果您对此提出更好的方法,我将非常高兴。
This is the Repository (I am using jsonplaceholder to get users):这是存储库(我使用 jsonplaceholder 来获取用户):
class HelpersRepository(val application: Application) {
val showProgress = MutableLiveData<Boolean>()
val helpersList = MutableLiveData<List<Helpers>>()
fun getAllHelpers() {
showProgress.value = true
val destinationService = ServiceBuilder.buildService(HelpersNetwork::class.java)
val requestCall = destinationService.getHelpers()
requestCall.enqueue(object : Callback<List<Helpers>>{
override fun onResponse(call: Call<List<Helpers>>, response: Response<List<Helpers>>) {
helpersList.value = response.body()
Log.d("response", "${helpersList.value}")
showProgress.value = false
}
override fun onFailure(call: Call<List<Helpers>>, t: Throwable) {
showProgress.value = false
Toast.makeText(application, "Error while fetching the data", Toast.LENGTH_SHORT).show()
}
})
}}
This is the ViewModel :这是ViewModel :
class HelpersViewModel(application: Application) : AndroidViewModel(application) {
private val repository = HelpersRepository(application)
val showProgress : LiveData<Boolean>
val helpersList : LiveData<List<Helpers>>
init {
this.showProgress = repository.showProgress
this.helpersList = repository.helpersList
}
fun getAllHelpers() {
repository.getAllHelpers()
}
}
And this is my Fragment :这是我的片段:
class HelpersFragment : Fragment() {
private lateinit var viewModel: HelpersViewModel
private lateinit var adapter: HelpersAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_helpers, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this).get(HelpersViewModel::class.java)
adapter = HelpersAdapter(requireContext())
rv_helpers.adapter = adapter
viewModel.getAllHelpers()
viewModel.showProgress.observe(viewLifecycleOwner, Observer {
if (it) {
pb_helpers.visibility = View.VISIBLE
} else {
pb_helpers.visibility = View.GONE
}
})
viewModel.helpersList.observe(viewLifecycleOwner, Observer {
adapter.setHelpersList(it)
})
}
}
when viewModel.getAllHelpers()
gets called in the fragment, the log displays the response three times (I am unable to copy the log as StackOverflow considers it as a spam).当在片段中调用
viewModel.getAllHelpers()
时,日志会显示响应三次(我无法复制日志,因为 StackOverflow 将其视为垃圾邮件)。
PS If I put viewModel.getAllHelpers()
inside a setOnclickListener
on button, then it only fetches data one time. PS 如果我将
viewModel.getAllHelpers()
放在按钮上的setOnclickListener
中,那么它只会获取一次数据。
I have searched everywhere but was unable to get the answer fitted to my question.我到处搜索,但无法得到适合我问题的答案。 I realize I am doing something wrong, but can't figure out exactly what
我意识到我做错了什么,但无法弄清楚到底是什么
I finally figured out that my problem was coming from choosing dark mode in MainActivity.我终于发现我的问题来自在 MainActivity 中选择了暗模式。 Turns out you must call:
结果你必须打电话:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
before setContentView()
in onCreate
在
onCreate
setContentView()
之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.