From c7b4d00da7b544fea6a1e9af44d9ffc3cca78d22 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 15 Jun 2020 18:02:07 -0400 Subject: [PATCH] Fix race condition loading bookmarks --- .../BookmarksTableViewController.swift | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift index 02e59b9531..18dc5cc300 100644 --- a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift +++ b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift @@ -45,13 +45,14 @@ class BookmarksTableViewController: EnhancedTableViewController { let request = Client.getBookmarks() mastodonController.run(request) { (response) in guard case let .success(statuses, pagination) = response else { fatalError() } - self.mastodonController.persistentContainer.addAll(statuses: statuses) - self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) - self.newer = pagination?.newer - self.older = pagination?.older - - DispatchQueue.main.async { - self.tableView.reloadData() + self.mastodonController.persistentContainer.addAll(statuses: statuses) { + self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) + self.newer = pagination?.newer + self.older = pagination?.older + + DispatchQueue.main.async { + self.tableView.reloadData() + } } } @@ -87,15 +88,16 @@ class BookmarksTableViewController: EnhancedTableViewController { mastodonController.run(request) { (response) in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older - self.mastodonController.persistentContainer.addAll(statuses: newStatuses) - let newIndexPaths = (self.statuses.count..<(self.statuses.count + newStatuses.count)).map { - IndexPath(row: $0, section: 0) - } - self.statuses.append(contentsOf: newStatuses.map { ($0.id, .unknown) }) - - DispatchQueue.main.async { - UIView.performWithoutAnimation { - self.tableView.insertRows(at: newIndexPaths, with: .automatic) + self.mastodonController.persistentContainer.addAll(statuses: newStatuses) { + let newIndexPaths = (self.statuses.count..<(self.statuses.count + newStatuses.count)).map { + IndexPath(row: $0, section: 0) + } + self.statuses.append(contentsOf: newStatuses.map { ($0.id, .unknown) }) + + DispatchQueue.main.async { + UIView.performWithoutAnimation { + self.tableView.insertRows(at: newIndexPaths, with: .automatic) + } } } }