当前位置:网站首页>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.
边栏推荐
- 谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
- 硬件设备计算存储及数据交互杂谈
- 虹科分享|如何用移动目标防御技术防范未知因素
- 命名实体识别-模型:BERT-MRC
- 信奥学习规划 信息学竞赛之路(2022.07.31)
- SQL injection Less54 (limited number of SQL injection + union injection)
- 新产品如何进行网络推广?
- 继承的注意事项
- Interview Blitz 69: Is TCP Reliable?Why?
- Thinking and Implementation of Object Cache Service
猜你喜欢

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

NIO programming

Redis五种数据类型简介

【读书笔记->数据分析】02 数据分析准备

NIO编程

基于单片机GSM的防火防盗系统的设计
![[Reading Notes -> Data Analysis] 02 Data Analysis Preparation](/img/e7/258daf851746cb043f301437ee3bbe.png)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation

Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads

ICML2022 | 深入研究置换敏感的图神经网络

How to Design High Availability and High Performance Middleware - Homework
随机推荐
消息队列存储消息数据的MySQL表格
C# Rectangle basic usage and picture cutting
[1161. The maximum sum of elements in the layer]
date命令
@JsonFormat(pattern="yyyy-MM-dd") time difference problem
One line of code to solve CoreData managed object properties change in SwiftUI problem of animation effects
LeetCode--打家劫舍问题
无状态与有状态的区别
面试突击69:TCP 可靠吗?为什么?
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
继承和友元,静态成员的关系
面试突击69:TCP 可靠吗?为什么?
SQL注入 Less42(POST型堆叠注入)
Shell常用脚本:Nexus批量上传本地仓库脚本
How to import a Golang external package and use it?
IPD流程专业术语
WindowInsetsControllerCompat is simple to use
如何设计高可用高性能中间件 - 作业
一文概述:VPN的基本模型及业务类型
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)