diff --git a/Tusker/CoreData/MastodonCachePersistentStore.swift b/Tusker/CoreData/MastodonCachePersistentStore.swift index d404fcd8..450eb74f 100644 --- a/Tusker/CoreData/MastodonCachePersistentStore.swift +++ b/Tusker/CoreData/MastodonCachePersistentStore.swift @@ -118,5 +118,20 @@ class MastodonCachePersistentStore: NSPersistentContainer { 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) } + } + } } diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index f93743a6..e5f1061a 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -63,14 +63,13 @@ class NotificationsTableViewController: EnhancedTableViewController { 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.older = pagination?.older - DispatchQueue.main.async { - self.tableView.reloadData() + self.mastodonController.persistentContainer.addAll(notifications: notifications) { + DispatchQueue.main.async { + self.tableView.reloadData() + } } } } @@ -141,15 +140,14 @@ class NotificationsTableViewController: EnhancedTableViewController { IndexPath(row: $0, section: 0) } 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 - DispatchQueue.main.async { - UIView.performWithoutAnimation { - self.tableView.insertRows(at: newIndexPaths, with: .automatic) + self.mastodonController.persistentContainer.addAll(notifications: newNotifications) { + DispatchQueue.main.async { + UIView.performWithoutAnimation { + self.tableView.insertRows(at: newIndexPaths, with: .automatic) + } } } } @@ -219,25 +217,24 @@ class NotificationsTableViewController: EnhancedTableViewController { 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 { self.newer = newer } - - DispatchQueue.main.async { - let newIndexPaths = (0..