Flush multi-process writes more aggressively.

This commit is contained in:
Matthew Chen 2019-01-24 10:22:15 -05:00
parent f9bbbbd0c9
commit e7b9f7da99
4 changed files with 59 additions and 16 deletions

View File

@ -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;

View File

@ -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<ConversationViewItem> _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];
}

View File

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

View File

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