Fix statuses/accounts updating

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

View File

@ -9,11 +9,15 @@
import Foundation import Foundation
import CoreData import CoreData
import Pachyderm import Pachyderm
import Combine
class MastodonCachePersistentStore: NSPersistentContainer { class MastodonCachePersistentStore: NSPersistentContainer {
private(set) lazy var backgroundContext = newBackgroundContext() private(set) lazy var backgroundContext = newBackgroundContext()
let statusSubject = PassthroughSubject<String, Never>()
let accountSubject = PassthroughSubject<String, Never>()
init(for controller: MastodonController) { init(for controller: MastodonController) {
let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")! let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")!
let model = NSManagedObjectModel(contentsOf: url)! let model = NSManagedObjectModel(contentsOf: url)!
@ -58,6 +62,7 @@ class MastodonCachePersistentStore: NSPersistentContainer {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?()
self.statusSubject.send(status.id)
} }
} }
@ -68,6 +73,7 @@ class MastodonCachePersistentStore: NSPersistentContainer {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?()
statuses.forEach { self.statusSubject.send($0.id) }
} }
} }
@ -98,6 +104,7 @@ class MastodonCachePersistentStore: NSPersistentContainer {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?()
self.accountSubject.send(account.id)
} }
} }
@ -108,6 +115,7 @@ class MastodonCachePersistentStore: NSPersistentContainer {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?()
accounts.forEach { self.accountSubject.send($0.id) }
} }
} }

View File

@ -69,11 +69,6 @@ class BaseStatusTableViewCell: UITableViewCell {
private var statusUpdater: Cancellable? private var statusUpdater: Cancellable?
private var accountUpdater: Cancellable? private var accountUpdater: Cancellable?
deinit {
statusUpdater?.cancel()
accountUpdater?.cancel()
}
override func awakeFromNib() { override func awakeFromNib() {
super.awakeFromNib() super.awakeFromNib()
@ -95,20 +90,27 @@ class BaseStatusTableViewCell: UITableViewCell {
} }
open func createObserversIfNecessary() { open func createObserversIfNecessary() {
// todo: KVO on StatusMO for this? if statusUpdater == nil {
// if statusUpdater == nil { statusUpdater = mastodonController.persistentContainer.statusSubject
// statusUpdater = mastodonController.cache.statusSubject .filter { [unowned self] in $0 == self.statusID }
// .filter { [unowned self] in $0.id == self.statusID } .receive(on: DispatchQueue.main)
// .receive(on: DispatchQueue.main) .sink { [unowned self] in
// .sink { [unowned self] in self.updateStatusState(status: $0) } if let status = self.mastodonController.persistentContainer.status(for: $0) {
// } self.updateStatusState(status: status)
// }
// if accountUpdater == nil { }
// accountUpdater = mastodonController.cache.accountSubject }
// .filter { [unowned self] in $0.id == self.accountID }
// .receive(on: DispatchQueue.main) if accountUpdater == nil {
// .sink { [unowned self] in self.updateUI(account: $0) } 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) { func updateUI(statusID: String, state: StatusState) {

View File

@ -47,13 +47,16 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell {
override func createObserversIfNecessary() { override func createObserversIfNecessary() {
super.createObserversIfNecessary() super.createObserversIfNecessary()
// todo: use KVO on reblogger account? if rebloggerAccountUpdater == nil {
// if rebloggerAccountUpdater == nil { rebloggerAccountUpdater = mastodonController.persistentContainer.accountSubject
// rebloggerAccountUpdater = mastodonController.cache.accountSubject .filter { [unowned self] in $0 == self.rebloggerID }
// .filter { [unowned self] in $0.id == self.rebloggerID } .receive(on: DispatchQueue.main)
// .receive(on: DispatchQueue.main) .sink { [unowned self] in
// .sink { [unowned self] in self.updateRebloggerLabel(reblogger: $0) } if let reblogger = self.mastodonController.persistentContainer.account(for: $0) {
// } self.updateRebloggerLabel(reblogger: reblogger)
}
}
}
} }
override func updateUI(statusID: String, state: StatusState) { override func updateUI(statusID: String, state: StatusState) {
@ -226,7 +229,7 @@ extension TimelineStatusTableViewCell: TableViewSwipeActionProvider {
return return
} }
completion(true) completion(true)
mastodonController.cache.add(status: status) mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
} }
}) })
} }