Fix statuses/accounts updating

This commit is contained in:
Shadowfacts 2020-05-02 12:45:28 -04:00
parent 2cba168804
commit 5786c24846
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 40 additions and 27 deletions

View File

@ -9,11 +9,15 @@
import Foundation
import CoreData
import Pachyderm
import Combine
class MastodonCachePersistentStore: NSPersistentContainer {
private(set) lazy var backgroundContext = newBackgroundContext()
let statusSubject = PassthroughSubject<String, Never>()
let accountSubject = PassthroughSubject<String, Never>()
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) }
}
}

View File

@ -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) {

View File

@ -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)
}
})
}