# When would I not want to share a subscription?

**URL:** <https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934>\
**Category:** RxSwift\
**Created:** [January 27, 2018, 6:27am UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934 "2018-01-27T06:27:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultimatefilipino](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ultimatefilipino/32/81494_2.png) [@ultimatefilipino](https://forums.kodeco.com/u/ultimatefilipino)\
**Post date:** [January 27, 2018, 6:27am UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/1 "2018-01-27T06:27:21Z")

</div>

After reading the example in chapter 6 (page 117 in RxSwift\_v1.1), it seems to me that almost all subscriptions should be shared. Is this a valid thought and if so, in what scenarios would I not want to share a subscription?

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [January 28, 2018, 1:29am UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/2 "2018-01-28T01:29:41Z")

</div>

@ultimatefilipino. Thanks very much for your question!

I took a look, and found the following paragraph that might help answer your question:

The rule of thumb about sharing operators is that it’s safe to use share() with observables that do not complete, or if you guarantee no new subscriptions will be made after completion. If you want piece of mind use share(replay: 1) - you’ll learn more about this in Chapter 8, “Transforming Operators in Practice.”

So my guess would be to share in the above circumstances.

I hope this helps!

All the best!

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [January 28, 2018, 9:32am UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/3 "2018-01-28T09:32:32Z")

</div>

hey @ultimatefilipino, in many (if not most) cases sharing an observable sequence to many observers is what you want. The main point here is that the first subscription is the one creating the observable and therefore performing the side effects - for example in the case you are uploading a file and you’re observing the progress of the upload. Let’s imagine you have a function `uploadFile(filePath: String) -> Observable<Double>` which takes a file path and uploads a file while emitting the upload progress as `Double` values. In this case each time you call `uploadFile()` you’re effectively uploading one more file, while if you use `share()` on a sinlge `uploadFile()` then you’re uploading one file but sharing the progress with more observers… I hope this example is useful

---

<div class="post-metadata">

**Author:** ![ultimatefilipino](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ultimatefilipino/32/81494_2.png) [@ultimatefilipino](https://forums.kodeco.com/u/ultimatefilipino)\
**Post date:** [January 28, 2018, 3:30pm UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/4 "2018-01-28T15:30:45Z")

</div>

Let me see if I can sum this up in an if/else block

if obserableHasNoSideEffects || onlyRunObservableOnce {  
// share observable  
} else {  
// don’t share observable  
}

Is that correct?

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [January 28, 2018, 3:53pm UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/5 "2018-01-28T15:53:15Z")

</div>

yeah that sounds good to me

---

<div class="post-metadata">

**Author:** ![ultimatefilipino](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ultimatefilipino/32/81494_2.png) [@ultimatefilipino](https://forums.kodeco.com/u/ultimatefilipino)\
**Post date:** [January 28, 2018, 11:26pm UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/6 "2018-01-28T23:26:23Z")

</div>

Thanks so much for your help!

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [July 12, 2018, 10:28pm UTC](https://forums.kodeco.com/t/when-would-i-not-want-to-share-a-subscription/38934/7 "2018-07-12T22:28:10Z")

</div>

Closed on July 12, 2018, 10:28pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
