Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the point that Kotlin objects pay more attention to?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what are the points that Kotlin objects pay more attention to?" the editor shows you the operation process through actual cases, the method of operation is simple and fast, and is practical. I hope that this article "what are the points that Kotlin objects pay more attention to" can help you solve the problem.

Background

An existing StateFlow and its monitoring

Privateval stateFlow = MutableStateFlow (kotlin.Pair ("abc", ArrayList () GlobalScope.launch {stateFlow.collect {/ / do something}}

Update ArrayList and try emit

GlobalScope.launch {stateFlow.value.second.add ("test") stateFlow.emit (stateFlow.value)}

In fact, collect will not be called

Reason

The real implementer of MutableStateFlow is StateFlowImpl, and the emit method code is as follows:

Override suspend fun emit (value: t) {this.value = value}

View the set method of value:

Public override var value: t get () = NULL.unbox (_ state.value) set (value) {updateState (null, value?: NULL)} private fun updateState (expectedState: Any?, newState: Any): Boolean {var curSequence = 0 var curSlots: Array? = this.slots / / benign race We will not use it synchronized (this) {val oldState = _ state.value if (expectedState! = null & & oldState! = expectedState) return false / / CAS support if (oldState = = newState) return true / / Don't do anything if value is not changing, but CAS-> true _ state.value = newState curSequence = sequence. Omit some code}}

The condition of "if (oldState = = newState) return true" is true because it is the same object before and after emit, so if it is not the same object before and after emit, this problem can be solved?

Another problem

Try the following code when emit:

GlobalScope.launch {stateFlow.value.apply {stateFlow.emit (kotlin.Pair (first, second))}}

In fact, the above code still doesn't solve the problem, because kotlin.Pair overrides the equals method by default to view kotlin.Pair decompiled's Java file.

Public final class Pair {public int hashCode () {Object var10000 = this.first; int var1 = (var10000! = null? Var10000.hashCode (): 0) * 31; Object var10001 = this.second; return var1 + (var10001! = null? Var10001.hashCode (): 0);} public boolean equals (@ Nullable Object var1) {if (this! = var1) {if (var1 instanceof Te.Pair) {Te.Pair var2 = (Te.Pair) var1; if (Intrinsics.areEqual (this.first, var2.first) & & Intrinsics.areEqual (this.second, var2.second)) {return true }} return false;} else {return true;}

The Intrinsics.areEqual code is as follows:

Public static boolean areEqual (Object first, Object second) {return first = = null? Second = = null: first.equals (second);}

Therefore, even though the pair object itself is different, because kotlin overrides the equals method by default, and pair.first is the same as pair.second, the condition "if (oldState = = newState) return true" is established.

Solution.

Since the StateFlow source code cannot be modified and is required by a specific scenario, the judgment condition cannot be changed to "= =" of kotlin; therefore, you can use android.util.Pair or custom java Pair class.

This is the end of the content about "what is the most important thing to pay attention to in Kotlin objects?" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report