当前位置:网站首页>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.
边栏推荐
- /etc/sysconfig/network-scripts configure the network card
- Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
- 虹科分享|如何用移动目标防御技术防范未知因素
- 类和对象:上
- Network security - crack WiFi through handshake packets (detailed tutorial)
- LeetCode--打家劫舍问题
- 字符编码和浮点型计算精度丢失问题
- 2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
- (26)Blender源码分析之顶层菜单的关于菜单
猜你喜欢

消息队列存储消息数据的MySQL表格
![[Reading Notes -> Data Analysis] 02 Data Analysis Preparation](/img/e7/258daf851746cb043f301437ee3bbe.png)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation

(26) About menu of the top menu of Blender source code analysis

vector的基本实现

How to Design High Availability and High Performance Middleware - Homework

精心总结十三条建议,帮你创建更合适的MySQL索引

清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架

NIO programming

面试突击69:TCP 可靠吗?为什么?

Design of Fire and Anti-theft System Based on Single Chip GSM
随机推荐
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
[QNX Hypervisor 2.2 User Manual]9.16 system
[MATLAB project combat] LDPC-BP channel coding
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
[1161. The maximum sum of elements in the layer]
/etc/sysconfig/network-scripts configure the network card
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
Redis五种数据类型简介
【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
thymeleaf迭代map集合
类和对象:中
10大主流3D建模技术
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
Flink 1.13(八)CDC
The difference between /usr/local/bin and /usr/bin
Named Entity Recognition - Model: BERT-MRC
消息队列存储消息数据的MySQL表格
日常--Kali开启SSH(详细教程)
SQL injection Less38 (stack injection)
面试突击69:TCP 可靠吗?为什么?