# Chapter 8 share vs share(replay: 1) question

**URL:** <https://forums.kodeco.com/t/chapter-8-share-vs-share-replay-1-question/75293>\
**Category:** RxSwift\
**Created:** [July 2, 2019, 3:59am UTC](https://forums.kodeco.com/t/chapter-8-share-vs-share-replay-1-question/75293 "2019-07-02T03:59:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josscii](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/josscii/32/110937_2.png) [@josscii](https://forums.kodeco.com/u/josscii)\
**Post date:** [July 2, 2019, 3:59am UTC](https://forums.kodeco.com/t/chapter-8-share-vs-share-replay-1-question/75293/1 "2019-07-02T03:59:54Z")

</div>

description from the book:

> URLSession.rx.response(request:) sends your request to the server, and upon receiving the response, emits a .next event just once with the returned data, and then completes.

> In this situation, if the observable completes and then you subscribe to it again, that will create a new subscription and will ﬁre another identical request to the server.

> To prevent situations like this, you use share(replay:scope:). This operator keeps a buffer of the last replay elements emitted and feeds them to any newly subscribed observers. Therefore, if your request has completed and a new observer subscribes to the shared sequence (via share(replay:scope:)), it will immediately receive the buffered response from the previously-executed network request.

I wrote some demo code to mimic the networking code, just as how `URLSession.rx.response(request:)` is work.

```swift
let just = Observable.just("nothing here")
    .flatMap { _ in
        return Observable<Int>.create { observer in
            print("start")
            DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
                observer.onNext(4)
                observer.onCompleted()
            })
            
            return Disposables.create()
        }
    }
// .share()
    .share(replay: 1)

//1
just.subscribe(onNext: {
    print($0)
}, onCompleted: {
    print("completed")
})

//2
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    just.subscribe(onNext: {
        print($0)
    }, onCompleted: {
        print("completed")
    })
}

//3
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    just.subscribe(onNext: {
        print($0)
    }, onCompleted: {
        print("completed")
    })
}

```

the output:

```auto
start
4
4
completed
completed
start
4
completed

```

When the inner observable completes, the third subscription restart the whole process other than replay the last value.

Why? Is there any bugs in my code or the description in this book is wrong?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 21, 2019, 10:32pm UTC](https://forums.kodeco.com/t/chapter-8-share-vs-share-replay-1-question/75293/2 "2019-08-21T22:32:33Z")

</div>

@josscii Do you still have issues with this?

---

<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:** [December 15, 2019, 7:59pm UTC](https://forums.kodeco.com/t/chapter-8-share-vs-share-replay-1-question/75293/3 "2019-12-15T19:59:56Z")

</div>

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