Fix race condition

pull/177/head
nielsandriesse 5 years ago
parent e374d1d0c8
commit a49e686ca0

@ -3,6 +3,7 @@
final class ConversationTitleView : UIView { final class ConversationTitleView : UIView {
private let thread: TSThread private let thread: TSThread
private var currentStatus: Status? { didSet { updateSubtitleForCurrentStatus() } } private var currentStatus: Status? { didSet { updateSubtitleForCurrentStatus() } }
private var handledMessageTimestamps: Set<NSNumber> = []
// MARK: Types // MARK: Types
private enum Status : Int { private enum Status : Int {
@ -112,6 +113,7 @@ final class ConversationTitleView : UIView {
@objc private func handleMessageSentNotification(_ notification: Notification) { @objc private func handleMessageSentNotification(_ notification: Notification) {
guard let timestamp = notification.object as? NSNumber else { return } guard let timestamp = notification.object as? NSNumber else { return }
setStatusIfNeeded(to: .messageSent, forMessageWithTimestamp: timestamp) setStatusIfNeeded(to: .messageSent, forMessageWithTimestamp: timestamp)
handledMessageTimestamps.insert(timestamp)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.clearStatusIfNeededForMessageWithTimestamp(timestamp) self.clearStatusIfNeededForMessageWithTimestamp(timestamp)
} }
@ -123,6 +125,7 @@ final class ConversationTitleView : UIView {
} }
private func setStatusIfNeeded(to status: Status, forMessageWithTimestamp timestamp: NSNumber) { private func setStatusIfNeeded(to status: Status, forMessageWithTimestamp timestamp: NSNumber) {
guard !handledMessageTimestamps.contains(timestamp) else { return }
var uncheckedTargetInteraction: TSInteraction? = nil var uncheckedTargetInteraction: TSInteraction? = nil
thread.enumerateInteractions { interaction in thread.enumerateInteractions { interaction in
guard interaction.timestamp == timestamp.uint64Value else { return } guard interaction.timestamp == timestamp.uint64Value else { return }

@ -220,6 +220,10 @@ typedef enum : NSUInteger {
@property (nonatomic) NSMutableArray<LKMention *> *mentions; @property (nonatomic) NSMutableArray<LKMention *> *mentions;
@property (nonatomic) NSString *oldText; @property (nonatomic) NSString *oldText;
// Status bar updating
/// Used to avoid duplicate status bar updates.
@property (nonatomic) NSMutableSet<NSNumber *> *handledMessageTimestamps;
@end @end
#pragma mark - #pragma mark -
@ -5396,6 +5400,7 @@ typedef enum : NSUInteger {
{ {
NSNumber *timestamp = (NSNumber *)notification.object; NSNumber *timestamp = (NSNumber *)notification.object;
[self setProgressIfNeededTo:1.0f forMessageWithTimestamp:timestamp]; [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) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void) {
[self hideProgressIndicatorViewForMessageWithTimestamp:timestamp]; [self hideProgressIndicatorViewForMessageWithTimestamp:timestamp];
}); });
@ -5409,6 +5414,11 @@ typedef enum : NSUInteger {
- (void)setProgressIfNeededTo:(float)progress forMessageWithTimestamp:(NSNumber *)timestamp - (void)setProgressIfNeededTo:(float)progress forMessageWithTimestamp:(NSNumber *)timestamp
{ {
if ([self.handledMessageTimestamps contains:^BOOL(NSNumber *t) {
return [t isEqual:timestamp];
}]) {
return;
}
__block TSInteraction *targetInteraction; __block TSInteraction *targetInteraction;
[self.thread enumerateInteractionsUsingBlock:^(TSInteraction *interaction) { [self.thread enumerateInteractionsUsingBlock:^(TSInteraction *interaction) {
if (interaction.timestamp == timestamp.unsignedLongLongValue) { if (interaction.timestamp == timestamp.unsignedLongLongValue) {

Loading…
Cancel
Save