From ee252c02e948d25ca3238bd66cd9d6d4f3e0e0cd Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 19 Jan 2020 23:14:51 -0500 Subject: [PATCH] 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. --- Tusker/Views/Status/BaseStatusTableViewCell.swift | 8 ++++---- Tusker/Views/Status/TimelineStatusTableViewCell.swift | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index a34f8d09..6d43c28e 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -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) } } } diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index 6e115f6c..6ffa7b64 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -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()