From a49e686ca0df33aaef2780acd732493d4276b04a Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Thu, 30 Apr 2020 10:04:14 +1000 Subject: [PATCH] Fix race condition --- Signal/src/Loki/Components/ConversationTitleView.swift | 3 +++ .../ConversationView/ConversationViewController.m | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/Signal/src/Loki/Components/ConversationTitleView.swift b/Signal/src/Loki/Components/ConversationTitleView.swift index 6f63668a1..aaad7022c 100644 --- a/Signal/src/Loki/Components/ConversationTitleView.swift +++ b/Signal/src/Loki/Components/ConversationTitleView.swift @@ -3,6 +3,7 @@ final class ConversationTitleView : UIView { private let thread: TSThread private var currentStatus: Status? { didSet { updateSubtitleForCurrentStatus() } } + private var handledMessageTimestamps: Set = [] // MARK: Types private enum Status : Int { @@ -112,6 +113,7 @@ final class ConversationTitleView : UIView { @objc private func handleMessageSentNotification(_ notification: Notification) { guard let timestamp = notification.object as? NSNumber else { return } setStatusIfNeeded(to: .messageSent, forMessageWithTimestamp: timestamp) + handledMessageTimestamps.insert(timestamp) DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.clearStatusIfNeededForMessageWithTimestamp(timestamp) } @@ -123,6 +125,7 @@ final class ConversationTitleView : UIView { } private func setStatusIfNeeded(to status: Status, forMessageWithTimestamp timestamp: NSNumber) { + guard !handledMessageTimestamps.contains(timestamp) else { return } var uncheckedTargetInteraction: TSInteraction? = nil thread.enumerateInteractions { interaction in guard interaction.timestamp == timestamp.uint64Value else { return } diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index a798d972b..b1e812c87 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -220,6 +220,10 @@ typedef enum : NSUInteger { @property (nonatomic) NSMutableArray *mentions; @property (nonatomic) NSString *oldText; +// Status bar updating +/// Used to avoid duplicate status bar updates. +@property (nonatomic) NSMutableSet *handledMessageTimestamps; + @end #pragma mark - @@ -5396,6 +5400,7 @@ typedef enum : NSUInteger { { NSNumber *timestamp = (NSNumber *)notification.object; [self setProgressIfNeededTo:1.0f forMessageWithTimestamp:timestamp]; + [self.handledMessageTimestamps addObject:timestamp]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void) { [self hideProgressIndicatorViewForMessageWithTimestamp:timestamp]; }); @@ -5409,6 +5414,11 @@ typedef enum : NSUInteger { - (void)setProgressIfNeededTo:(float)progress forMessageWithTimestamp:(NSNumber *)timestamp { + if ([self.handledMessageTimestamps contains:^BOOL(NSNumber *t) { + return [t isEqual:timestamp]; + }]) { + return; + } __block TSInteraction *targetInteraction; [self.thread enumerateInteractionsUsingBlock:^(TSInteraction *interaction) { if (interaction.timestamp == timestamp.unsignedLongLongValue) {