If you don't understand it yet, look at the simplified code below:
// Article Error Codeđź™…Simplification for (i in 0..100) { // Simulate Generated Data flow<Int> { emit(i) }.debounce(500) // This is an invalid limit because emit of flow only executes once... .collect { println("----------------->>> $it") } } Copy Code
So the right code logic we want is this:
flow<Int> { for (i in 0..100) { // Simulate Generated Data emit(i) } }.debounce(500) // Here is a valid flow limit .collect { println("----------------->>> $it") } Copy Code
Okay, students can compare the above two pieces of code again. The for loop is the input data we simulated. The for loop is two completely different logic inside and outside - no more explanations.
What you think is the correct writing
For this type of business logic of input boxes, flow ing alone cannot be used because it cannot be written out.Some children wanted to stand up and shout, "How can't I write it out? You talk nonsense, let me write it". In one operation, the following code was written:
// Error code written by childđź™… flow<Editable> { editText.doAfterTextChanged { text -> emit(text) // This is a mistake. emit cannot be written in an internal class } }.debounce(500) .collect { println("----------------->>> $it") } Copy Code
Wrong place I wrote a comment, emit is a suspend pending function, can't be written in the internal class, code compiles directly fail.
True correct writing
First of all, let me mention a point of knowledge that you will recall.The flow in RxJava is divided into cold and hot Observable, right.If you say anything, it's still hot and cold.Mother, I suggest you stir-fry the pot back.
In the use of RxJava, one of the reasons for the misuse and abuse of RxJava is that you do not pay attention to distinguishing between hot and cold streams.
Here I will simply explain the hot and cold flow in two sentences, not RxJava.
-
Cold Flow: Upstream launches only when the observer subscribes
-
Heat Flow: Upstream data is emitted with or without observers
flow is cold
Explain directly with notes:
flow { // Launch data }.collect { /* Only collect or collectLast subscribed to the stream, Code blocks inside upstream flow will execute! */ } Copy Code
Now this kid, calm down and think about the business scene of the input box.That is, data is sent whenever the EditText text changes, whether or not there are subscribers.Then we should use heat flow to solve the problem.
StateFlow heat flow arrives
Look directly at the code, guys:
// Define a global StateFlow private val _etState = MutableStateFlow("") override fun onCreate(savedInstanceState: Bundle?) { et.doAfterTextChanged { text -> // Write data into stream _etState.value = (text ?: "").toString() } lifecycleScope.launch { #### **How do I do a good job of interview assault and plan my study direction?** Interview problem sets can help you identify gaps, study in a targeted way, and prepare for going to the factory later.But if you just look at it once, you don't learn and delve into it.This interview question will be of limited help to you.Ultimately, it depends on the advanced technical level to speak. Online Learning Android There is a lot of data, but if the knowledge you have learned is not systematic, you will only have a taste when you encounter problems and no further research, it will be difficult to achieve real technological improvement.It is suggested to make a learning plan first, and associate the knowledge points according to the learning plan to form a systematic knowledge system. The direction of learning is easy to plan, but if you only learn in fragmentation, your improvement will be slow. At the same time, I also collect and sort out the interview questions of Tencent, Ali, Huawei, Millet and other companies in 2020, combing the requirements and technical points of the interview into a large and complete " Android Architect Interview Xmind(It actually takes a lot more energy than expected), including**Knowledge Vein + Branch Details**. ![image](https://img-blog.csdnimg.cn/img_convert/2b315308936dd72fd39cea90e1a8e245.png) When these technical frameworks are built, advanced tutorials are organized, which are much more effective than your own fragmentation learning. ![image](https://img-blog.csdnimg.cn/img_convert/4f9a83db456189363b8909bc96739103.png) **[CodeChina Open Source Project: Android Summary of Learning Notes+Mobile Architecture Video+True subject for factory interview+Project Actual Source](https://codechina.csdn.net/m0_60958482/android_p7)** In dump...(img-pYyK8We3-1630943725993)] When these technical frameworks are built, advanced tutorials are organized, which are much more effective than your own fragmentation learning. [Outer Chain Picture Transfer in Progress...(img-0tNY2hgS-1630943725995)] **[CodeChina Open Source Project: Android Summary of Learning Notes+Mobile Architecture Video+True subject for factory interview+Project Actual Source](https://codechina.csdn.net/m0_60958482/android_p7)** Online Learning Android There is a lot of data, but if the knowledge you have learned is not systematic, you will only have a taste when you encounter problems and no further research, it will be difficult to achieve real technological improvement.I hope this systematic technical system can provide you with a direction reference.