pull/243/head
nielsandriesse 5 years ago
parent 5308a2b51b
commit f8dd62ca43

@ -216,16 +216,12 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol
isObservingDatabase = isViewVisible && CurrentAppContext().isAppForegroundAndActive() isObservingDatabase = isViewVisible && CurrentAppContext().isAppForegroundAndActive()
} }
private func updateYDBThreadMapping() { private func reload() {
AssertIsOnMainThread()
uiDatabaseConnection.beginLongLivedReadTransaction() uiDatabaseConnection.beginLongLivedReadTransaction()
uiDatabaseConnection.read { transaction in uiDatabaseConnection.read { transaction in
self.threads.update(with: transaction) self.threads.update(with: transaction)
} }
}
private func reload() {
AssertIsOnMainThread()
updateYDBThreadMapping()
threadViewModelCache.removeAll() threadViewModelCache.removeAll()
tableView.reloadData() tableView.reloadData()
emptyStateView.isHidden = (threadCount != 0) emptyStateView.isHidden = (threadCount != 0)
@ -243,17 +239,13 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol
} }
return return
} }
// Guard for changes made in Notification Service Extension // If changes were made in a different process (e.g. the Notification Service Extension) the thread mapping can be out of date
// Crashes may happen if there is some modification in NSE // at this point, causing the app to crash. The code below prevents that by force syncing the database before proceeding.
// The YapDBModificationNotification cannot cross process if notifications.count > 0 {
// So the thread mapping won't update itself if DB modification happened in other process like NSE if let firstChangeSet = notifications[0].userInfo {
// With these code we can sync the mapping before asking for changes from YapDB let firstSnapshot = firstChangeSet[YapDatabaseSnapshotKey] as! UInt64
if (notifications.count > 0) { if threads.snapshotOfLastUpdate != firstSnapshot - 1 {
if let firstChangeset = notifications[0].userInfo { return reload()
let firstSnapshot = firstChangeset[YapDatabaseSnapshotKey] as! UInt64
if (self.threads.snapshotOfLastUpdate != firstSnapshot - 1) {
reload()
return
} }
} }
} }
@ -283,7 +275,6 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol
@objc private func handleApplicationDidBecomeActiveNotification(_ notification: Notification) { @objc private func handleApplicationDidBecomeActiveNotification(_ notification: Notification) {
updateIsObservingDatabase() updateIsObservingDatabase()
updateYDBThreadMapping()
} }
@objc private func handleApplicationWillResignActiveNotification(_ notification: Notification) { @objc private func handleApplicationWillResignActiveNotification(_ notification: Notification) {

@ -157,7 +157,6 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic
- (BOOL)isMainAppAndActive - (BOOL)isMainAppAndActive
{ {
if (![NSThread isMainThread]) { return NO; }
return [UIApplication sharedApplication].applicationState == UIApplicationStateActive; return [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
} }

Loading…
Cancel
Save