Retrofit2 with liveData makes api calls twice the second time they are called, three times the third etc
You're observing the livedata in onClickListener! It means everytime you click on that button, a new observer (with viewLifecycleOwner) will attach to the liveData and keeps being active as long as the fragment's view is running. So every click will add another observer which will never be removed, unless the view is destroyed. Use observe when there are no actions (e.g. you're observing a live data in onViewCreated which will always get notified of any changes), on actions (like clicks) simply get the value of LiveData and do something with it. Like this
override fun onViewCreated(){
//...
btnYoti.setOnClickListener {
viewModel.postYoti()
}
//Add a getter to your VM to prevent direct access to rep
viewModel.repository.postYotiLiveData.observe(viewLifecycleOwner, {
println("Hello World")
}
}
}
On the other hand, you can use a LiveEvent to prevent redundant prints caused by resuming the fragment