Sharing Policies
val sharedFlow = flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.WhileSubscribed()
)
Slide 89
Slide 89 text
While Subscribed
• Active as long as external scope is alive
• Remains as long as there are collectors.
Slide 90
Slide 90 text
Properties
Active as long as external scope is alive
Slide 91
Slide 91 text
Properties
Active as long as external scope is alive
sharedFlow.collect { }
Subscriber
Slide 92
Slide 92 text
flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.WhileSubscribed()
)
Properties
Active as long as external scope is alive
Slide 93
Slide 93 text
Properties
Active as long as external scope is alive
sharedFlow.collect { }
externalScope.cancel()
Slide 94
Slide 94 text
Properties
Active as long as external scope is alive
sharedFlow.collect { }
externalScope.cancel()
Complete Exceptionally
Slide 95
Slide 95 text
Properties
Remains as long as there are collectors.
Slide 96
Slide 96 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
Slide 97
Slide 97 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
Slide 98
Slide 98 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
job.cancel()
Slide 99
Slide 99 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
job.cancel()
Slide 100
Slide 100 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job = launch { sharedFlow.onCompletion { }.collect { } }
job.cancel()
Slide 101
Slide 101 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
val job1 = launch { sharedFlow.collect { } }
val job2 = launch { sharedFlow.collect { } }
Slide 102
Slide 102 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
job1.cancel()
val job2 = launch { sharedFlow.collect { } }
Slide 103
Slide 103 text
Properties
Remains as long as there are collectors.
val sharedFlow = flow.onCompletion { }.shareIn(…)
job1.cancel()
val job2 = launch { sharedFlow.collect { } }
Remain Active
Slide 104
Slide 104 text
Properties
• Active as long as external scope is alive
• Remains as long as there are collectors.
Slide 105
Slide 105 text
Sharing Policies
• While Subscribed
• Eagerly
• Lazily
Slide 106
Slide 106 text
Eagerly
flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.Eagerly()
)
Slide 107
Slide 107 text
Eagerly
Start producer eagerly and never stop
flow
.onStart { println("ON START") }
.shareIn(
...
started = SharingStarted.Eagerly)
Slide 108
Slide 108 text
Eagerly
Start producer eagerly and never stop
flow
.onStart { println("ON START") }
.shareIn(
...
started = SharingStarted.Eagerly)
Slide 109
Slide 109 text
Eagerly
Start producer eagerly and never stop
flow
.onStart { println("ON START") }
.shareIn(
...
started = SharingStarted.Eagerly)
ON START
Slide 110
Slide 110 text
Eagerly
Start producer eagerly and never stop
flow
.onComplete { println("ON COMPLETE”) }
.shareIn(
...
started = SharingStarted.Eagerly)
Slide 111
Slide 111 text
Eagerly
Start producer eagerly and never stop
flow
.onComplete { println("ON COMPLETE”) }
.shareIn(
...
started = SharingStarted.Eagerly)
externalScope.cancel()
Slide 112
Slide 112 text
Eagerly
Start producer eagerly and never stop
flow
.onComplete { println("ON COMPLETE”) }
.shareIn(
...
started = SharingStarted.Eagerly)
Never stops
externalScope.cancel()
Slide 113
Slide 113 text
Eagerly
Start producer eagerly and never stop
Slide 114
Slide 114 text
Sharing Policies
• While Subscribed
• Eagerly
• Lazily
Slide 115
Slide 115 text
Lazily
Start sharing after the first subscriber appears and never stop
Slide 116
Slide 116 text
Lazily
flow.shareIn(
externalScope,
replay = 1,
started = SharingStarted.Lazily
)
Lazily
Start sharing after the first subscriber appears and never stop
Slide 125
Slide 125 text
Sharing Policies
• While Subscribed
• Active while there are active subscribers.
• Eagerly
• Start producer eagerly and never stop
• Lazily
• Start after the first subscriber appears and never stop
Slide 126
Slide 126 text
Buffer Overflow Strategies
Slide 127
Slide 127 text
Shared Flow
Buffer
Slide 128
Slide 128 text
Shared Flow
Producer Consumer
Slide 129
Slide 129 text
Shared Flow
Producer Consumer
Generating events fast
Slide 130
Slide 130 text
Shared Flow
Producer Consumer
Listening to events
with delay
Slide 131
Slide 131 text
Shared Flow
Producer Consumer
Slide 132
Slide 132 text
Shared Flow
Producer Consumer
Slide 133
Slide 133 text
Shared Flow
Producer Consumer
What happens when it is full?
Slide 134
Slide 134 text
Buffering Overflow Strategies
• Suspend
• Drop oldest
• Drop latest