当前位置:网站首页>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.
边栏推荐
- 助力数字政府建设,中科三方构建域名安全保障体系
- Matlab / ArcGIS 处理GPM全球月均降水数据
- NgRx 里 first 和 take(1) 操作符的区别
- 命名实体识别-模型:BERT-MRC
- 力扣2326、197
- I don't know what to do with sync issues
- [MATLAB project combat] LDPC-BP channel coding
- To help the construction of digital government, the three parties of China Science and Technology build a domain name security system
- Usage of mysql having
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
猜你喜欢
逐步手撕轮播图3(保姆级教程)
推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
Drawing process of hand-drawn map of scenic spots
什么是客户画像管理?
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Flink 1.13(八)CDC
/etc/sysconfig/network-scripts 配置网卡
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
随机推荐
[QNX Hypervisor 2.2用户手册]9.16 system
IPD process terminology
编写方法将一个数组扁平化并且去重和递增排序
/etc/sysconfig/network-scripts configure the network card
【FPGA教程案例43】图像案例3——通过verilog实现图像sobel边缘提取,通过MATLAB进行辅助验证
清华大学陈建宇教授团队 | 基于接触丰富机器人操作的接触安全强化学习框架
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
LeetCode--打家劫舍问题
【1161. 最大层内元素和】
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
输入输出优化
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
vector的基本实现
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
leetcode:126. 单词接龙 II
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
类和对象:上
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
虚继承的原理
Input and output optimization