I dont understand where in mvvm model object should be store.
For e.g i have app
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
viewModel.userScore.observe(this, Observer { it->
score_view.text = it.toString()
})
score_bt.setOnClickListener {
viewModel.scorePoint()
}
}
}
class MyViewModel: ViewModel() {
val _userScore = MutableLiveData<Int>()
val userScore: LiveData<Int>
get() = _userScore
init {
_userScore.value = 1
}
fun scorePoint(){
_userScore.value = (_userScore.value)?.plus(1)
}
}
class Game {
val score = 0
}
when user click button the score increase. I want to store the score in object class Game. Where should the object be stored and how to connect the object with viewmodel, because I think that viewmodel shouldn't contain the object. To be clear I don't expect to stored that object when user turn off app.
question from:
https://stackoverflow.com/questions/65850979/where-object-should-be-store-in-mvvm 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…