当前位置:网站首页>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全球月均降水数据
- [MATLAB project combat] LDPC-BP channel coding
- 2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
- Daily--Kali opens SSH (detailed tutorial)
- 南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
- SQL injection Less42 (POST type stack injection)
- 继承的注意事项
- WindowInsetsControllerCompat简单使用
- leetcode:126. 单词接龙 II
猜你喜欢

日常--Kali开启SSH(详细教程)

一文带你了解 Grafana 最新开源项目 Mimir 的前世今生

Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)

网络安全--通过握手包破解WiFi(详细教程)

如何设计高可用高性能中间件 - 作业

消息队列消息存储设计(架构实战营 模块八作业)

cobaltstrike
![[MATLAB project combat] LDPC-BP channel coding](/img/37/4777e4d05cb2dbb1865f1d05ae9878.png)
[MATLAB project combat] LDPC-BP channel coding

【Acwing】第62场周赛 题解

消息队列存储消息数据的MySQL表格
随机推荐
景区手绘地图的绘制流程
Flutter教程之四年开发经验的高手给的建议
SQL injection Less54 (limited number of SQL injection + union injection)
Redis五种数据类型简介
The role of /etc/resolv.conf
面试突击69:TCP 可靠吗?为什么?
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
Components of TypeScript
Daily--Kali opens SSH (detailed tutorial)
[1161. The maximum sum of elements in the layer]
【MATLAB项目实战】LDPC-BP信道编码
vector的基本实现
[QNX Hypervisor 2.2 User Manual]9.16 system
力扣二叉树
(26)Blender源码分析之顶层菜单的关于菜单
[微服务]分布式事务解决方案-Seata
mysql having的用法
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发