diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 00e35e94d..a8bf0367c 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1237,6 +1237,8 @@ static NSTimeInterval launchStartedAt; [self preheatDatabaseViews]; + [self.primaryStorage touchDbAsync]; + // Try to update account attributes every time we upgrade. if ([self.tsAccountManager isRegistered]) { AppVersion *appVersion = AppVersion.sharedInstance; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewModel.m b/Signal/src/ViewControllers/ConversationView/ConversationViewModel.m index 3998ae779..59dbbd414 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewModel.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewModel.m @@ -337,6 +337,42 @@ static const int kYapDatabaseRangeMaxLength = 25000; - (void)viewDidLoad { [self addNotificationListeners]; + + [self touchDbAsync]; +} + +- (void)touchDbAsync +{ + // See comments in primaryStorage.touchDbAsync. + [self.primaryStorage touchDbAsync]; + + id _Nullable firstViewItem = self.viewItems.firstObject; + if (firstViewItem) { + __weak ConversationViewModel *weakSelf = self; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), + ^{ + ConversationViewModel *_Nullable strongSelf = weakSelf; + if (!strongSelf) { + return; + } + if (CurrentAppContext().reportedApplicationState == UIApplicationStateBackground) { + return; + } + [strongSelf.editingDatabaseConnection + readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + // Reload the interaction to ensure it still exists. + TSInteraction *_Nullable interaction = + [TSInteraction fetchObjectWithUniqueID:firstViewItem.interaction.uniqueId + transaction:transaction]; + if (!interaction) { + // Interaction appears to have been deleted. + return; + } + [interaction touchWithTransaction:transaction]; + }]; + }); + } } - (void)dealloc @@ -963,22 +999,8 @@ static const int kYapDatabaseRangeMaxLength = 25000; [self.delegate conversationViewModelDidUpdate:ConversationUpdate.reloadUpdate]; } -- (void)touchDbAsync -{ - OWSLogInfo(@""); - - // There appears to be a bug in YapDatabase that sometimes delays modifications - // made in another process (e.g. the SAE) from showing up in other processes. - // There's a simple workaround: a trivial write to the database flushes changes - // made from other processes. - [self.editingDatabaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [transaction setObject:[NSUUID UUID].UUIDString forKey:@"conversation_view_noop_mod" inCollection:@"temp"]; - }]; -} - - (void)applicationWillEnterForeground:(NSNotification *)notification { - // See comments in touchDbAsync. [self touchDbAsync]; } diff --git a/SignalServiceKit/src/Storage/OWSPrimaryStorage.h b/SignalServiceKit/src/Storage/OWSPrimaryStorage.h index 779f817ec..dd1cf65b3 100644 --- a/SignalServiceKit/src/Storage/OWSPrimaryStorage.h +++ b/SignalServiceKit/src/Storage/OWSPrimaryStorage.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // #import "OWSStorage.h" @@ -42,6 +42,10 @@ extern NSString *const OWSUIDatabaseConnectionNotificationsKey; + (void)protectFiles; +#pragma mark - Misc. + +- (void)touchDbAsync; + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Storage/OWSPrimaryStorage.m b/SignalServiceKit/src/Storage/OWSPrimaryStorage.m index 3fa3aba82..8afb8cf7b 100644 --- a/SignalServiceKit/src/Storage/OWSPrimaryStorage.m +++ b/SignalServiceKit/src/Storage/OWSPrimaryStorage.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // #import "OWSPrimaryStorage.h" @@ -431,6 +431,21 @@ void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) return OWSPrimaryStorage.sharedManager.dbReadWriteConnection; } +#pragma mark - Misc. + +- (void)touchDbAsync +{ + OWSLogInfo(@""); + + // There appears to be a bug in YapDatabase that sometimes delays modifications + // made in another process (e.g. the SAE) from showing up in other processes. + // There's a simple workaround: a trivial write to the database flushes changes + // made from other processes. + [self.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [transaction setObject:[NSUUID UUID].UUIDString forKey:@"conversation_view_noop_mod" inCollection:@"temp"]; + }]; +} + @end NS_ASSUME_NONNULL_END