Fix retain cycle in timeline cell cache observers

The use an unowned reference to self because when the cell is deinit'd,
the Combine observers will be cancelled.
This commit is contained in:
Shadowfacts 2020-01-19 23:14:51 -05:00
parent 2f630f2f8f
commit ee252c02e9
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 7 additions and 6 deletions

View File

@ -100,16 +100,16 @@ class BaseStatusTableViewCell: UITableViewCell {
open func createObserversIfNecessary() {
if statusUpdater == nil {
statusUpdater = mastodonController.cache.statusSubject
.filter { $0.id == self.statusID }
.filter { [unowned self] in $0.id == self.statusID }
.receive(on: DispatchQueue.main)
.sink(receiveValue: updateStatusState(status:))
.sink { [unowned self] in self.updateStatusState(status: $0) }
}
if accountUpdater == nil {
accountUpdater = mastodonController.cache.accountSubject
.filter { $0.id == self.accountID }
.filter { [unowned self] in $0.id == self.accountID }
.receive(on: DispatchQueue.main)
.sink(receiveValue: updateUI(account:))
.sink { [unowned self] in self.updateUI(account: $0) }
}
}

View File

@ -49,9 +49,9 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell {
if rebloggerAccountUpdater == nil {
rebloggerAccountUpdater = mastodonController.cache.accountSubject
.filter { $0.id == self.rebloggerID }
.filter { [unowned self] in $0.id == self.rebloggerID }
.receive(on: DispatchQueue.main)
.sink(receiveValue: updateRebloggerLabel(reblogger:))
.sink { [unowned self] in self.updateRebloggerLabel(reblogger: $0) }
}
}
@ -95,6 +95,7 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell {
}
func updateTimestamp() {
guard superview != nil else { return }
guard let status = mastodonController.cache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
timestampLabel.text = status.createdAt.timeAgoString()