当前位置:网站首页>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.
边栏推荐
- 基于单片机GSM的防火防盗系统的设计
- When can I use PushGateway
- 面试突击69:TCP 可靠吗?为什么?
- mysql having的用法
- 面试突击69:TCP 可靠吗?为什么?
- Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
- Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
- WindowInsetsControllerCompat is simple to use
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- lua入门案例实战123DIY
猜你喜欢
随机推荐
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
网络安全--通过握手包破解WiFi(详细教程)
Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
Difference Between Stateless and Stateful
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
Flutter教程之四年开发经验的高手给的建议
继承的注意事项
/etc/sysconfig/network-scripts configure the network card
qlib量化源码分析:qlib/qlib/contrib/model/gbdt.py
SQL注入 Less47(报错注入) 和Less49(时间盲注)
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
[QNX Hypervisor 2.2 User Manual]9.16 system
The difference between /usr/local/bin and /usr/bin
什么是动态规划,什么是背包问题
二叉树遍历非递归程序 -- 使用栈模拟系统栈
[QNX Hypervisor 2.2用户手册]9.16 system
NIO编程
Keil nRF52832 download failed
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
NgRx 里 first 和 take(1) 操作符的区别




![[1161. The maximum sum of elements in the layer]](/img/59/7810f425431779aa719458038ea0b3.png)




