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

@ -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) {

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

Loading…
Cancel
Save