当前位置:网站首页>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.
边栏推荐
- LeetCode--打家劫舍问题
- Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
- How to import a Golang external package and use it?
- Interview assault 69: TCP reliable?Why is that?
- 消息队列存储消息数据的MySQL表格
- 什么时候可以使用 PushGateway
- 面试突击69:TCP 可靠吗?为什么?
- 基于单片机GSM的防火防盗系统的设计
- Web API Introduction and Types
- 南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
猜你喜欢

Flink 1.13(八)CDC

Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决

Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)

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

硬件设备计算存储及数据交互杂谈
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team

什么是动态规划,什么是背包问题

/etc/sysconfig/network-scripts configure the network card

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

精心总结十三条建议,帮你创建更合适的MySQL索引
随机推荐
SQL注入 Less38(堆叠注入)
[AMEX] LGBM Optuna美国运通信用卡欺诈赛 kaggle
面试突击69:TCP 可靠吗?为什么?
SQL injection Less38 (stack injection)
字符编码和浮点型计算精度丢失问题
Matlab / ArcGIS 处理GPM全球月均降水数据
cobaltstrike
Weekly Summary
lua入门案例实战123DIY
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
How to import a Golang external package and use it?
TFC CTF 2022 WEB Diamand WriteUp
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
Matlab / Arcgis处理nc数据
SQL injection Less54 (limited number of SQL injection + union injection)
LeetCode--打家劫舍问题
虚继承的原理
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
Keil nRF52832 download failed
精心总结十三条建议,帮你创建更合适的MySQL索引