Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to understand complicated sharedflow expression?

Posted on November 11, 2022 By

Solution 1 :

Each Flow operator (besides stateIn and shareIn) creates a new cold flow that wraps the one it’s called on. When the downstream flow is collected, it runs through the actions of its upstream operators.

debounce immediately collects from upstream when it is collected, but delays re-emitting the values downstream by the amount of time you specify. It drops the pending value if a new one arrives during the delay. Its purpose is to filter out rapid changes to simplify what the downstream collectors have to deal with. Your upstream shared flow will immediately see a subscriber at the moment the downstream flow is collected.

onEach adds a side-effect that occurs each time the upstream flow emits.

shareIn makes the Flow hot, but WhileSubscribed limits when the flow is hot to only while there is at least one subscriber. This is so we don’t waste resources fetching or monitoring data when there is nothing using it. The 5000L means it waits 5000ms before turning off after it loses its last subscriber. This avoids needlessly turning off the flow when we lose a last subscriber but get a new subscriber quickly. On Android, this is a frequent occurrence when the screen rotates, because the subscribers will be destroyed, but new subscribers to the same things will be created once the Activity is recreated in the new orientation. We want to avoid having to restart the flow from scratch so it can display the latest data quickly.

Problem :

I would like to fully understand what happens here.

sharedFlow.debounce(250)
            .onEach(::updateGroupingStrategy)
            .shareIn(viewModelScope, SharingStarted.WhileSubscribed(5000L), replay = 1)

debounce

Returns a flow that mirrors the original flow, but filters out values that are followed by the newer values within the given timeout. The latest value is always emitted.
+
debounce helps to detect the state when no new data is submitted for some time, effectively allowing you to process a data when the input is completed.

From my perspective, I delay subscribing sharedflow for 250L ms or I only subscribe values every and after 250L?

onEach

* Returns a flow that invokes the given [action] **before** each value of the upstream flow is emitted downstream.

From my perspective, quite don’t understand it. First return flow then start subscribing, assign value for it?

READ  [FIXED] android - Start App From Service in BackGround in oreo and newer
Powered by Inline Related Posts

.shareIn

Sharing is started when the first subscriber appears, immediately stops when the last subscriber disappears (by default), keeping the replay cache forever (by default).
It has the following optional parameters:
stopTimeoutMillis — configures a delay (in milliseconds) between the disappearance of the last subscriber and the stopping of the sharing coroutine. It defaults to zero (stop immediately).

replay - the number of values replayed to new subscribers (cannot be negative, defaults to zero).

From my perspective, after there is not more subscriber I wait 5000L miliseconds then I make action? Replay I do not understand.

Comments

Comment posted by chrisChris

I jot it down to my android encyklopedia. Thanks man i really appreciate your detailed post. During learning I will come back to it once more.

Android Tags:android, asynchronous, flow, kotlin, kotlin-sharedflow

Post navigation

Previous Post: [FIXED] android – LottieAnimationView doesn’t show in my_layout.xml
Next Post: [FIXED] android – Firestore conditional array query

Related Posts

[FIXED] android – How do I add a blur effect at the right edge horizontal ListView to show that there is more content Android
[FIXED] android – Uncheck bottomnavigationmenu item when navigating to other fragment Android
[FIXED] java – Android Studio – How to read (Line) from a text file? Android
[FIXED] java – Recycleview delete data – display data problem Android
[FIXED] android – WebView ends up in infinite loop when going back if it follows an alternative link Android
[FIXED] android – How do you adb to multiinstance bluestacks 4? What is the adb-port of the secondary instances? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme