From 4e1e23282591db20e2d629ca882246dc2188f49d Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 22 Feb 2018 10:44:05 -0500 Subject: [PATCH] Flush writes from other processes. --- .../ConversationViewController.m | 16 +++++++++++++--- SignalServiceKit/src/Storage/OWSStorage.m | 10 ++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 0bc959291..238ccc786 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -4170,9 +4170,11 @@ typedef enum : NSUInteger { if (hasAddedNewItems) { NSIndexPath *_Nullable indexPathToShow = [self firstIndexPathAtPreviousLastUnreadTimestamp]; if (indexPathToShow) { - [self.collectionView scrollToItemAtIndexPath:indexPathToShow - atScrollPosition:UICollectionViewScrollPositionCenteredVertically - animated:YES]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.collectionView scrollToItemAtIndexPath:indexPathToShow + atScrollPosition:UICollectionViewScrollPositionCenteredVertically + animated:YES]; + }); } } self.previousLastUnreadTimestamp = nil; @@ -4307,6 +4309,14 @@ typedef enum : NSUInteger { [self ensureDynamicInteractions]; [self updateBackButtonUnreadCount]; [self updateNavigationBarSubtitleLabel]; + + // 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 setObject:[NSUUID UUID].UUIDString + forKey:@"conversation_view_noop_mod" + inCollection:@"temp"]; } #pragma mark - ConversationCollectionViewDelegate diff --git a/SignalServiceKit/src/Storage/OWSStorage.m b/SignalServiceKit/src/Storage/OWSStorage.m index b424b2aaa..6687f5e57 100644 --- a/SignalServiceKit/src/Storage/OWSStorage.m +++ b/SignalServiceKit/src/Storage/OWSStorage.m @@ -76,7 +76,10 @@ typedef NSData *_Nullable (^CreateDatabaseMetadataBlock)(void); OWSAssert(delegate); OWSAssert(delegate.areAllRegistrationsComplete || self.canWriteBeforeStorageReady); - OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__]; + OWSBackgroundTask *_Nullable backgroundTask = nil; + if (CurrentAppContext().isMainApp) { + backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__]; + } [super readWriteWithBlock:block]; backgroundTask = nil; } @@ -100,7 +103,10 @@ typedef NSData *_Nullable (^CreateDatabaseMetadataBlock)(void); OWSAssert(delegate); OWSAssert(delegate.areAllRegistrationsComplete || self.canWriteBeforeStorageReady); - __block OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__]; + __block OWSBackgroundTask *_Nullable backgroundTask = nil; + if (CurrentAppContext().isMainApp) { + backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__]; + } [super asyncReadWriteWithBlock:block completionQueue:completionQueue completionBlock:^{ if (completionBlock) { completionBlock();