From ebbfc7a13204e2907eb071b029f20e9fd171689d Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 6 May 2020 19:32:32 -0400 Subject: [PATCH] Fix race condition on loading notifications --- .../MastodonCachePersistentStore.swift | 15 ++++++ .../NotificationsTableViewController.swift | 49 +++++++++---------- 2 files changed, 38 insertions(+), 26 deletions(-) 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..