From f8dd62ca438309cce4f933bca6b5346fd28d68d8 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 31 Jul 2020 09:45:16 +1000 Subject: [PATCH] Clean --- Signal/src/Loki/View Controllers/HomeVC.swift | 29 +++++++------------ Signal/src/util/MainAppContext.m | 1 - 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index eec8fefc7..dfa9c5553 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -216,21 +216,17 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol isObservingDatabase = isViewVisible && CurrentAppContext().isAppForegroundAndActive() } - private func updateYDBThreadMapping() { + private func reload() { + AssertIsOnMainThread() uiDatabaseConnection.beginLongLivedReadTransaction() uiDatabaseConnection.read { transaction in self.threads.update(with: transaction) } - } - - private func reload() { - AssertIsOnMainThread() - updateYDBThreadMapping() threadViewModelCache.removeAll() tableView.reloadData() emptyStateView.isHidden = (threadCount != 0) } - + @objc private func handleYapDatabaseModifiedNotification(_ notification: Notification) { AssertIsOnMainThread() guard isObservingDatabase else { return } @@ -243,17 +239,13 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol } return } - // Guard for changes made in Notification Service Extension - // Crashes may happen if there is some modification in NSE - // The YapDBModificationNotification cannot cross process - // So the thread mapping won't update itself if DB modification happened in other process like NSE - // With these code we can sync the mapping before asking for changes from YapDB - if (notifications.count > 0) { - if let firstChangeset = notifications[0].userInfo { - let firstSnapshot = firstChangeset[YapDatabaseSnapshotKey] as! UInt64 - if (self.threads.snapshotOfLastUpdate != firstSnapshot - 1) { - reload() - return + // If changes were made in a different process (e.g. the Notification Service Extension) the thread mapping can be out of date + // at this point, causing the app to crash. The code below prevents that by force syncing the database before proceeding. + if notifications.count > 0 { + if let firstChangeSet = notifications[0].userInfo { + let firstSnapshot = firstChangeSet[YapDatabaseSnapshotKey] as! UInt64 + if threads.snapshotOfLastUpdate != firstSnapshot - 1 { + return reload() } } } @@ -283,7 +275,6 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol @objc private func handleApplicationDidBecomeActiveNotification(_ notification: Notification) { updateIsObservingDatabase() - updateYDBThreadMapping() } @objc private func handleApplicationWillResignActiveNotification(_ notification: Notification) { diff --git a/Signal/src/util/MainAppContext.m b/Signal/src/util/MainAppContext.m index e367efa31..9790ae493 100644 --- a/Signal/src/util/MainAppContext.m +++ b/Signal/src/util/MainAppContext.m @@ -157,7 +157,6 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic - (BOOL)isMainAppAndActive { - if (![NSThread isMainThread]) { return NO; } return [UIApplication sharedApplication].applicationState == UIApplicationStateActive; }