当前位置:网站首页>Compose principle - the view and the principle of two-way data binding
Compose principle - the view and the principle of two-way data binding
2022-08-01 00:03:00 【lost summer】
Foreword:
Compose is inherently declarative programming with MVVM features.So how can this feature be used and how to implement data binding?What is the principle of it?Then try it today.
One. Experiment Demo
The logic is very simple, it is to display a Text first, the content is aaa.Then open the thread for 5S and modify the data to bbb.
First version code first:
class ComposeActivity : ComponentActivity() {var uiState by mutableStateOf(Student())override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val student = Student()setContent {Greeting(uiState.name)}Thread {Thread.sleep(5000)uiState.name = "bbb"Log.i("test","student.name:${student.name}, mutableStateOf.name:${uiState.name}")}.start()}}@Composablefun Greeting(name: String) {Log.i("test","Greeting:${name}")Text(text = "Hello $name!")}data class Student(var name: String = "aaa",valid id: Int = 0)
It seems that there is no problem, but once it is running, there is no change after 5S, and the displayed content is still aaa.Why is this?
Comparing with the official example, it is found that there is a little typo. In the official example, when assigning value to the Model, the writing method is similar to the following:
uiState = uiState.copy(name = "bbb", id = 1)
And I wrote it like this:
uiState.name = "bbb"
After the correction, it really took effect. After 5S, the display content became bbb.
Why should I emphasize that I made a mistake here?The reason you may have guessed, since only copy takes effect, then the principle of binding is in it.(PS: In fact, just assign uiState to a new object)
2. Binding principle
Let's take a look at the java code after decompilation of this line of code: uiState = uiState.copy(name = "bbb", id = 1):
this$0.setUiState(this$0.getUiState().copy(com.xt.composeapp.LiveLiterals.ComposeActivityKt.INSTANCE.String$arg-0$call-copy$arg-0$call-$set-uiState$$fun-$anonymous$$arg-0$call-$init$$$this$call-start$fun-onCreate$class-ComposeActivity(), com.xt.composeapp.LiveLiterals.ComposeActivityKt.INSTANCE.Int$arg-1$call-copy$arg-0$call-$set-uiState$$fun-$anonymous$$arg-0$call-$init$$$this$call-start$fun-onCreate$class-ComposeActivity()));
The code is very long, but it doesn't really matter, just look at the top: the setUiState method.In kotlin, as long as you assign a value to an object, it will eventually be converted to calling the setXXX method.Then we enter the setUiState method to see.
public final void setUiState(@NotNull Student var1) {Intrinsics.checkNotNullParameter(var1, "");MutableState $this$setValue$iv = this.uiState$delegate;KProperty property$iv = null;int $i$f$setValue = false;$this$setValue$iv.setValue(var1);//1 this line of code}
The final conversion is to call the setValue method of $this$setValue$iv.So what is the first object?In fact, it is the MutableState we declared earlier:
private final MutableState uiState$delegate = SnapshotStateKt.mutableStateOf$default(new Student((String)null, 0, 3, (DefaultConstructorMarker)null), (SnapshotMutationPolicy)null, 2, (Object)null);
So the line of code 1 will eventually call the setValue method of MutableState.
MutableState is actually an interface, and the final implementation class is actually a generated observation object for Student.So when calling setValue, if the judgment value has changed, the outer layer will be notified to refresh the UI.
边栏推荐
- 编译型语言和解释型语言的区别
- NgRx 里 first 和 take(1) 操作符的区别
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- 一文概述:VPN的基本模型及业务类型
- Network security - crack WiFi through handshake packets (detailed tutorial)
- [QNX Hypervisor 2.2用户手册]9.16 system
- 【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
- 浏览器下载快捷方式到桌面(PWA)
- IPD process terminology
- 基于mysql的消息队列设计
猜你喜欢
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
UOS统信系统 - WindTerm使用
[1161. The maximum sum of elements in the layer]
Design of Fire and Anti-theft System Based on Single Chip GSM
消息队列存储消息数据的MySQL表格
游戏安全03:缓冲区溢出攻击简单解释
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
景区手绘地图的绘制流程
手写一个简单的web服务器(B/S架构)
Flink 1.13(八)CDC
随机推荐
输入输出优化
对象缓存服务的思考和实现
SQL注入 Less42(POST型堆叠注入)
Shell common script: Nexus batch upload local warehouse script
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
如何撰写出一篇优质的数码类好物推荐文
cobaltstrike
开源好用的 流程图绘制工具 drawio
zeno使用方法笔记
【1161. 最大层内元素和】
vim的基本使用-命令模式
类和对象:中
lua入门案例实战123DIY
Difference between first and take(1) operators in NgRx
Usage of mysql having
【读书笔记->数据分析】02 数据分析准备
什么是动态规划,什么是背包问题
TFC CTF 2022 WEB Diamand WriteUp
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects