From d01a52758bd8e8b9c11e4279752b5e5f1b88a877 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 19 Jun 2017 17:10:34 -0400 Subject: [PATCH] Respond to CR. // FREEBIE --- .../ConversationView/MessagesViewController.m | 13 +++---- Signal/src/util/ThreadUtil.h | 3 +- Signal/src/util/ThreadUtil.m | 38 ++++++++----------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m index 6ee81b6d8..d24c61223 100644 --- a/Signal/src/ViewControllers/ConversationView/MessagesViewController.m +++ b/Signal/src/ViewControllers/ConversationView/MessagesViewController.m @@ -332,8 +332,6 @@ typedef enum : NSUInteger { _composeOnOpen = keyboardOnViewAppearing; _callOnOpen = callOnViewAppearing; - [self.uiDatabaseConnection beginLongLivedReadTransaction]; - // We need to create the "unread indicator" before we mark // all messages as read. [self ensureDynamicInteractions]; @@ -540,6 +538,8 @@ typedef enum : NSUInteger { - (void)viewWillAppear:(BOOL)animated { + DDLogDebug(@"%@ viewWillAppear", self.tag); + // We need to update the dynamic interactions before we do any layout. [self ensureDynamicInteractions]; @@ -990,6 +990,8 @@ typedef enum : NSUInteger { - (void)viewWillDisappear:(BOOL)animated { + DDLogDebug(@"%@ viewWillDisappear", self.tag); + [super viewWillDisappear:animated]; [self toggleObservers:NO]; @@ -2221,10 +2223,6 @@ typedef enum : NSUInteger { // while updating the range and the dynamic interactions. [[NSNotificationCenter defaultCenter] removeObserver:self name:YapDatabaseModifiedNotification object:nil]; - // We need to `beginLongLivedReadTransaction` before we update our - // mapping in order to jump to the most recent commit. - [self.uiDatabaseConnection beginLongLivedReadTransaction]; - // We need to update the dynamic interactions after loading earlier messages, // since the unseen indicator may need to move or change. [self ensureDynamicInteractions]; @@ -2702,8 +2700,7 @@ typedef enum : NSUInteger { [ThreadUtil ensureDynamicInteractionsForThread:self.thread contactsManager:self.contactsManager blockingManager:self.blockingManager - readDBConnection:self.uiDatabaseConnection - writeDBConnection:self.editingDatabaseConnection + dbConnection:self.editingDatabaseConnection hideUnreadMessagesIndicator:self.hasClearedUnreadMessagesIndicator firstUnseenInteractionTimestamp:self.dynamicInteractions.firstUnseenInteractionTimestamp maxRangeSize:maxRangeSize]; diff --git a/Signal/src/util/ThreadUtil.h b/Signal/src/util/ThreadUtil.h index 3f4f28768..41c84ab78 100644 --- a/Signal/src/util/ThreadUtil.h +++ b/Signal/src/util/ThreadUtil.h @@ -83,8 +83,7 @@ NS_ASSUME_NONNULL_BEGIN + (ThreadDynamicInteractions *)ensureDynamicInteractionsForThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager blockingManager:(OWSBlockingManager *)blockingManager - readDBConnection:(YapDatabaseConnection *)readDBConnection - writeDBConnection:(YapDatabaseConnection *)writeDBConnection + dbConnection:(YapDatabaseConnection *)dbConnection hideUnreadMessagesIndicator:(BOOL)hideUnreadMessagesIndicator firstUnseenInteractionTimestamp:(nullable NSNumber *)firstUnseenInteractionTimestamp maxRangeSize:(int)maxRangeSize; diff --git a/Signal/src/util/ThreadUtil.m b/Signal/src/util/ThreadUtil.m index d68cba379..6ae76e02a 100644 --- a/Signal/src/util/ThreadUtil.m +++ b/Signal/src/util/ThreadUtil.m @@ -116,16 +116,14 @@ NS_ASSUME_NONNULL_BEGIN + (ThreadDynamicInteractions *)ensureDynamicInteractionsForThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager blockingManager:(OWSBlockingManager *)blockingManager - readDBConnection:(YapDatabaseConnection *)readDBConnection - writeDBConnection:(YapDatabaseConnection *)writeDBConnection + dbConnection:(YapDatabaseConnection *)dbConnection hideUnreadMessagesIndicator:(BOOL)hideUnreadMessagesIndicator firstUnseenInteractionTimestamp: (nullable NSNumber *)firstUnseenInteractionTimestampParameter maxRangeSize:(int)maxRangeSize { OWSAssert(thread); - OWSAssert(readDBConnection); - OWSAssert(writeDBConnection); + OWSAssert(dbConnection); OWSAssert(contactsManager); OWSAssert(blockingManager); OWSAssert(maxRangeSize > 0); @@ -135,23 +133,15 @@ NS_ASSUME_NONNULL_BEGIN ThreadDynamicInteractions *result = [ThreadDynamicInteractions new]; - const int kMaxBlockOfferOutgoingMessageCount = 10; - - __block OWSAddToContactsOfferMessage *existingAddToContactsOffer = nil; - __block OWSUnknownContactBlockOfferMessage *existingBlockOffer = nil; - __block TSUnreadIndicatorInteraction *existingUnreadIndicator = nil; - NSMutableArray *blockingSafetyNumberChanges = [NSMutableArray new]; - NSMutableArray *nonBlockingSafetyNumberChanges = [NSMutableArray new]; - __block TSMessage *firstMessage = nil; - __block NSUInteger outgoingMessageCount; - __block NSUInteger threadMessageCount; - __block long visibleUnseenMessageCount = 0; - __block TSInteraction *interactionAfterUnreadIndicator = nil; - __block NSUInteger missingUnseenSafetyNumberChangeCount = 0; - - [readDBConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + const int kMaxBlockOfferOutgoingMessageCount = 10; // Find any "dynamic" interactions and safety number changes. + __block OWSAddToContactsOfferMessage *existingAddToContactsOffer = nil; + __block OWSUnknownContactBlockOfferMessage *existingBlockOffer = nil; + __block TSUnreadIndicatorInteraction *existingUnreadIndicator = nil; + NSMutableArray *blockingSafetyNumberChanges = [NSMutableArray new]; + NSMutableArray *nonBlockingSafetyNumberChanges = [NSMutableArray new]; // We use different views for performance reasons. [[TSDatabaseView threadSpecialMessagesDatabaseView:transaction] enumerateRowsInGroup:thread.uniqueId @@ -196,6 +186,7 @@ NS_ASSUME_NONNULL_BEGIN } } + __block TSMessage *firstMessage = nil; [[transaction ext:TSMessageDatabaseViewExtensionName] enumerateRowsInGroup:thread.uniqueId usingBlock:^( @@ -210,9 +201,9 @@ NS_ASSUME_NONNULL_BEGIN } }]; - outgoingMessageCount = + NSUInteger outgoingMessageCount = [[TSDatabaseView threadOutgoingMessageDatabaseView:transaction] numberOfItemsInGroup:thread.uniqueId]; - threadMessageCount = + NSUInteger threadMessageCount = [[transaction ext:TSMessageDatabaseViewExtensionName] numberOfItemsInGroup:thread.uniqueId]; // Enumerate in reverse to count the number of messages @@ -221,6 +212,9 @@ NS_ASSUME_NONNULL_BEGIN // the messages view the position of the unread indicator, // so that it can widen its "load window" to always show // the unread indicator. + __block long visibleUnseenMessageCount = 0; + __block TSInteraction *interactionAfterUnreadIndicator = nil; + NSUInteger missingUnseenSafetyNumberChangeCount = 0; if (result.firstUnseenInteractionTimestamp != nil) { [[transaction ext:TSMessageDatabaseViewExtensionName] enumerateRowsInGroup:thread.uniqueId @@ -294,9 +288,7 @@ NS_ASSUME_NONNULL_BEGIN result.unreadIndicatorPosition = @(visibleUnseenMessageCount); } OWSAssert((result.firstUnseenInteractionTimestamp != nil) == (result.unreadIndicatorPosition != nil)); - }]; - [writeDBConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { BOOL shouldHaveBlockOffer = YES; BOOL shouldHaveAddToContactsOffer = YES;