# Combine ownership model: Subscription -\> Subscriber -\> Publisher

**URL:** <https://forums.kodeco.com/t/combine-ownership-model-subscription-subscriber-publisher/145753>\
**Category:** Combine: Asynchronous Programming with Swift\
**Created:** [August 7, 2021, 10:02pm UTC](https://forums.kodeco.com/t/combine-ownership-model-subscription-subscriber-publisher/145753 "2021-08-07T22:02:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![altairs99](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/altairs99/32/105896_2.png) [@altairs99](https://forums.kodeco.com/u/altairs99)\
**Post date:** [August 7, 2021, 10:02pm UTC](https://forums.kodeco.com/t/combine-ownership-model-subscription-subscriber-publisher/145753/1 "2021-08-07T22:02:54Z")

</div>

Hello @icanzilb ,

I have couple questions about ownership model between **Subscription** → **Subscriber** → **Publisher**. Sorry many letters 🙂

1. Subscriber (sink) will be alive since we do `.store(in: &subscriptions)`. But Is `Publisher` will also guaranteed to be alive until we cancel/release subscription? Since publisher is not retained in case below, only strong reference to subscription.

Basically question is to how guarantee Subscriber will not stop working due to dealloc of the publisher? E.g. is there any need additional private strong property to publisher?

```auto
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    api.stories()
      .receive(on: DispatchQueue.main)
      .catch { _ in Empty() }
      .sink { [weak self] result in
                self?.handle(result: result)
       }
      .store(in: &subscriptions)
  }

```

1. Same question in context of combining operators (e.g. `combineLatest`, `merge`):  
Will combineLatest/merge maintain strong reference to subscriber+publishers until we cancel/release subscription? Or do we need additional private strong properties to publishers?

```auto

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

let storiesPublisher =
    api.stories()
      .receive(on: DispatchQueue.main)
      .catch { _ in Empty() }
      .share()

let storiesCommentsPublisher =
    api.storiesComments()
      .receive(on: DispatchQueue.main)
      .catch { _ in Empty() }
      .share()

storiesPublisher
       .map { [unowned self] in self.mapStories($0) }
       .sink { [weak self] result in
                self?.handleStories(result: result)
       }
      .store(in: &subscriptions)
storiesCommentsPublisher
       .map { [unowned self] in self.mapComments($0) }
       .sink { [weak self] result in
                self?.handleStoriesComments(result: result)
       }
      .store(in: &subscriptions)

storiesPublisher
            .combineLatest(storiesCommentsPublisher)
            .map { [unowned self] stories, comments in
                self.handleStoriesAndStoriesCommentsCombined(stories: stories, comments: comments)
            }
            .assign(to: &$state)

}

```

1. Is it safe to use `unowned self` inside map/sink operator above? Assuming `self` strong referencing all subscriptions. Is there possibility for dangling state: (self is `nil`) and `sink/map` closures invoked?

---

<div class="post-metadata">

**Author:** ![altairs99](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/altairs99/32/105896_2.png) [@altairs99](https://forums.kodeco.com/u/altairs99)\
**Post date:** [August 9, 2021, 8:36am UTC](https://forums.kodeco.com/t/combine-ownership-model-subscription-subscriber-publisher/145753/2 "2021-08-09T08:36:09Z")

</div>

Hello @icanzilb. Never mind, I found in the book following:  
`Both system-provided subscribers conform to Cancellable, which means that your subscription code (e.g. the whole publisher, operators and subscriber call chain) returns a Cancellable object. Whenever you release that object from memory, it cancels the whole subscription and releases its resources from memory.`  
So I guess `Publisher` is considered as resources and should be retained.

Regarding the last question with `weak/unowned` I found your article with in-depth explanation:

> **[Using self, weak, and unowned in Combine](https://trycombine.com/posts/self-weak-unowned/)**
>
> Swift development related blog-posts. Combine, modern concurrency model, Instruments, and more.

Thanks, Dzmitry.

---

<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:** [August 20, 2021, 11:45am UTC](https://forums.kodeco.com/t/combine-ownership-model-subscription-subscriber-publisher/145753/3 "2021-08-20T11:45:21Z")

</div>

Yes, normal Swift memory management rules apply - the cancellables are there only to memory manage the lifetime of subscriptions; since subscriptions run asynchronously Combine needs a way to know when to release a subscription and that happens when your release the cancellable from memory (or call cancel())
