forked from shadowfacts/Tusker
Fix race condition on loading notifications
This commit is contained in:
parent
aa625a41f5
commit
ebbfc7a132
|
@ -118,5 +118,20 @@ class MastodonCachePersistentStore: NSPersistentContainer {
|
||||||
accounts.forEach { self.accountSubject.send($0.id) }
|
accounts.forEach { self.accountSubject.send($0.id) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addAll(notifications: [Pachyderm.Notification], completion: (() -> Void)? = nil) {
|
||||||
|
backgroundContext.perform {
|
||||||
|
let statuses = notifications.compactMap { $0.status }
|
||||||
|
let accounts = notifications.map { $0.account }
|
||||||
|
statuses.forEach { self.upsert(status: $0, incrementReferenceCount: true) }
|
||||||
|
accounts.forEach { self.upsert(account: $0) }
|
||||||
|
if self.backgroundContext.hasChanges {
|
||||||
|
try! self.backgroundContext.save()
|
||||||
|
}
|
||||||
|
completion?()
|
||||||
|
statuses.forEach { self.statusSubject.send($0.id) }
|
||||||
|
accounts.forEach { self.accountSubject.send($0.id) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,14 +63,13 @@ class NotificationsTableViewController: EnhancedTableViewController {
|
||||||
|
|
||||||
self.groups.append(contentsOf: groups)
|
self.groups.append(contentsOf: groups)
|
||||||
|
|
||||||
self.mastodonController.persistentContainer.addAll(statuses: notifications.compactMap { $0.status })
|
|
||||||
self.mastodonController.persistentContainer.addAll(accounts: notifications.map { $0.account })
|
|
||||||
|
|
||||||
self.newer = pagination?.newer
|
self.newer = pagination?.newer
|
||||||
self.older = pagination?.older
|
self.older = pagination?.older
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
self.mastodonController.persistentContainer.addAll(notifications: notifications) {
|
||||||
self.tableView.reloadData()
|
DispatchQueue.main.async {
|
||||||
|
self.tableView.reloadData()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,15 +140,14 @@ class NotificationsTableViewController: EnhancedTableViewController {
|
||||||
IndexPath(row: $0, section: 0)
|
IndexPath(row: $0, section: 0)
|
||||||
}
|
}
|
||||||
self.groups.append(contentsOf: groups)
|
self.groups.append(contentsOf: groups)
|
||||||
|
|
||||||
self.mastodonController.persistentContainer.addAll(statuses: newNotifications.compactMap { $0.status })
|
|
||||||
self.mastodonController.persistentContainer.addAll(accounts: newNotifications.map { $0.account })
|
|
||||||
|
|
||||||
self.older = pagination?.older
|
self.older = pagination?.older
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
self.mastodonController.persistentContainer.addAll(notifications: newNotifications) {
|
||||||
UIView.performWithoutAnimation {
|
DispatchQueue.main.async {
|
||||||
self.tableView.insertRows(at: newIndexPaths, with: .automatic)
|
UIView.performWithoutAnimation {
|
||||||
|
self.tableView.insertRows(at: newIndexPaths, with: .automatic)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -219,25 +217,24 @@ class NotificationsTableViewController: EnhancedTableViewController {
|
||||||
|
|
||||||
self.groups.insert(contentsOf: groups, at: 0)
|
self.groups.insert(contentsOf: groups, at: 0)
|
||||||
|
|
||||||
self.mastodonController.persistentContainer.addAll(statuses: newNotifications.compactMap { $0.status })
|
|
||||||
self.mastodonController.persistentContainer.addAll(accounts: newNotifications.map { $0.account })
|
|
||||||
|
|
||||||
if let newer = pagination?.newer {
|
if let newer = pagination?.newer {
|
||||||
self.newer = newer
|
self.newer = newer
|
||||||
}
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
self.mastodonController.persistentContainer.addAll(notifications: newNotifications) {
|
||||||
let newIndexPaths = (0..<groups.count).map {
|
DispatchQueue.main.async {
|
||||||
IndexPath(row: $0, section: 0)
|
let newIndexPaths = (0..<groups.count).map {
|
||||||
|
IndexPath(row: $0, section: 0)
|
||||||
|
}
|
||||||
|
UIView.performWithoutAnimation {
|
||||||
|
self.tableView.insertRows(at: newIndexPaths, with: .automatic)
|
||||||
|
}
|
||||||
|
|
||||||
|
self.refreshControl?.endRefreshing()
|
||||||
|
|
||||||
|
// maintain the current position in the list (don't scroll to top)
|
||||||
|
self.tableView.scrollToRow(at: IndexPath(row: newNotifications.count, section: 0), at: .top, animated: false)
|
||||||
}
|
}
|
||||||
UIView.performWithoutAnimation {
|
|
||||||
self.tableView.insertRows(at: newIndexPaths, with: .automatic)
|
|
||||||
}
|
|
||||||
|
|
||||||
self.refreshControl?.endRefreshing()
|
|
||||||
|
|
||||||
// maintain the current position in the list (don't scroll to top)
|
|
||||||
self.tableView.scrollToRow(at: IndexPath(row: newNotifications.count, section: 0), at: .top, animated: false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue