From 5786c24846f0499895b2962755cc898de4a8b52d Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 2 May 2020 12:45:28 -0400 Subject: [PATCH] Fix statuses/accounts updating --- .../MastodonCachePersistentStore.swift | 8 ++++ .../Status/BaseStatusTableViewCell.swift | 40 ++++++++++--------- .../Status/TimelineStatusTableViewCell.swift | 19 +++++---- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Tusker/CoreData/MastodonCachePersistentStore.swift b/Tusker/CoreData/MastodonCachePersistentStore.swift index ff2aad4b..d404fcd8 100644 --- a/Tusker/CoreData/MastodonCachePersistentStore.swift +++ b/Tusker/CoreData/MastodonCachePersistentStore.swift @@ -9,11 +9,15 @@ import Foundation import CoreData import Pachyderm +import Combine class MastodonCachePersistentStore: NSPersistentContainer { private(set) lazy var backgroundContext = newBackgroundContext() + let statusSubject = PassthroughSubject() + let accountSubject = PassthroughSubject() + init(for controller: MastodonController) { let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")! let model = NSManagedObjectModel(contentsOf: url)! @@ -58,6 +62,7 @@ class MastodonCachePersistentStore: NSPersistentContainer { try! self.backgroundContext.save() } completion?() + self.statusSubject.send(status.id) } } @@ -68,6 +73,7 @@ class MastodonCachePersistentStore: NSPersistentContainer { try! self.backgroundContext.save() } completion?() + statuses.forEach { self.statusSubject.send($0.id) } } } @@ -98,6 +104,7 @@ class MastodonCachePersistentStore: NSPersistentContainer { try! self.backgroundContext.save() } completion?() + self.accountSubject.send(account.id) } } @@ -108,6 +115,7 @@ class MastodonCachePersistentStore: NSPersistentContainer { try! self.backgroundContext.save() } completion?() + accounts.forEach { self.accountSubject.send($0.id) } } } diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index e5fc77b3..8e5ecad5 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -69,11 +69,6 @@ class BaseStatusTableViewCell: UITableViewCell { private var statusUpdater: Cancellable? private var accountUpdater: Cancellable? - deinit { - statusUpdater?.cancel() - accountUpdater?.cancel() - } - override func awakeFromNib() { super.awakeFromNib() @@ -95,20 +90,27 @@ class BaseStatusTableViewCell: UITableViewCell { } open func createObserversIfNecessary() { - // todo: KVO on StatusMO for this? -// if statusUpdater == nil { -// statusUpdater = mastodonController.cache.statusSubject -// .filter { [unowned self] in $0.id == self.statusID } -// .receive(on: DispatchQueue.main) -// .sink { [unowned self] in self.updateStatusState(status: $0) } -// } -// -// if accountUpdater == nil { -// accountUpdater = mastodonController.cache.accountSubject -// .filter { [unowned self] in $0.id == self.accountID } -// .receive(on: DispatchQueue.main) -// .sink { [unowned self] in self.updateUI(account: $0) } -// } + if statusUpdater == nil { + statusUpdater = mastodonController.persistentContainer.statusSubject + .filter { [unowned self] in $0 == self.statusID } + .receive(on: DispatchQueue.main) + .sink { [unowned self] in + if let status = self.mastodonController.persistentContainer.status(for: $0) { + self.updateStatusState(status: status) + } + } + } + + if accountUpdater == nil { + accountUpdater = mastodonController.persistentContainer.accountSubject + .filter { [unowned self] in $0 == self.accountID } + .receive(on: DispatchQueue.main) + .sink { [unowned self] in + if let account = self.mastodonController.persistentContainer.account(for: $0) { + self.updateUI(account: account) + } + } + } } func updateUI(statusID: String, state: StatusState) { diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index 019fdaec..afffdfc5 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -47,13 +47,16 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { override func createObserversIfNecessary() { super.createObserversIfNecessary() - // todo: use KVO on reblogger account? -// if rebloggerAccountUpdater == nil { -// rebloggerAccountUpdater = mastodonController.cache.accountSubject -// .filter { [unowned self] in $0.id == self.rebloggerID } -// .receive(on: DispatchQueue.main) -// .sink { [unowned self] in self.updateRebloggerLabel(reblogger: $0) } -// } + if rebloggerAccountUpdater == nil { + rebloggerAccountUpdater = mastodonController.persistentContainer.accountSubject + .filter { [unowned self] in $0 == self.rebloggerID } + .receive(on: DispatchQueue.main) + .sink { [unowned self] in + if let reblogger = self.mastodonController.persistentContainer.account(for: $0) { + self.updateRebloggerLabel(reblogger: reblogger) + } + } + } } override func updateUI(statusID: String, state: StatusState) { @@ -226,7 +229,7 @@ extension TimelineStatusTableViewCell: TableViewSwipeActionProvider { return } completion(true) - mastodonController.cache.add(status: status) + mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false) } }) }