viewmodel----01----viewmodel的初认识

ViewModel的诞生:

1. 瞬态数据丢失,屏幕旋转就会重新创建activity

2. 异步调用的内存泄漏 (分线程请求网络,在请求过程中)

3. 类膨胀提高维护难度和测试难度

ViewModel的作用:

1. 使视图和数据能够分离,也能保持通信

view  <-----> viewModel <-----> Model

 

不要向ViewModel中传入Context,会导致内存泄漏

如果要使用context,请使用AndroidViewModel中的Application

 

posted @ 2021-11-26 14:31  涂妖教  阅读(246)  评论(0)    收藏  举报