Hello @icanzilb ,
I have couple questions about ownership model between Subscription → Subscriber → Publisher. Sorry many letters ![]()
- Subscriber (sink) will be alive since we do
.store(in: &subscriptions). But IsPublisherwill 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?
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)
}
- 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?
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)
}
- Is it safe to use
unowned selfinside map/sink operator above? Assumingselfstrong referencing all subscriptions. Is there possibility for dangling state: (self isnil) andsink/mapclosures invoked?