From 81d5adc8c8a45605c8291a1e3ceb6da2d35d6b30 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 20 May 2019 10:40:39 +1000 Subject: [PATCH] Fix threading issue & unnecessary updating --- .../ConversationView/ConversationViewController.m | 7 ++++++- SignalServiceKit/src/Contacts/TSThread.m | 9 ++++++--- SignalServiceKit/src/Loki/Extensions/Notification.swift | 8 ++++++-- .../Loki/Extensions/SSKProtoPrekeyBundleMessage.swift | 6 +----- SignalServiceKit/src/Loki/Messages/OWSEphemeralMessage.m | 4 ++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 8dab2a244..c07f79d14 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -407,7 +407,7 @@ typedef enum : NSUInteger { object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThreadFriendRequestStatusChangedNotification:) - name:@"threadFriendRequestStatusChanged" + name:NSNotification.threadFriendRequestStatusChanged object:nil]; } @@ -474,7 +474,12 @@ typedef enum : NSUInteger { - (void)handleThreadFriendRequestStatusChangedNotification:(NSNotification *)notification { + // Check thread + NSString *threadID = (NSString *)notification.object; + if (![threadID isEqualToString:self.thread.uniqueId]) { return; } + // Ensure thread instance is up to date [self.thread reload]; + // Update UI [self.viewItems.lastObject clearCachedLayoutState]; // Presumably a cell with a friend request view [self resetContentAndLayout]; [self updateInputToolbar]; diff --git a/SignalServiceKit/src/Contacts/TSThread.m b/SignalServiceKit/src/Contacts/TSThread.m index 5d76a75d6..6c99dcc9b 100644 --- a/SignalServiceKit/src/Contacts/TSThread.m +++ b/SignalServiceKit/src/Contacts/TSThread.m @@ -703,12 +703,15 @@ ConversationColorName const kConversationColorName_Default = ConversationColorNa self.friendRequestStatus = friendRequestStatus; if (transaction == nil) { [self save]; + [self.dbReadWriteConnection flushTransactionsWithCompletionQueue:dispatch_get_main_queue() completionBlock:^{ + [NSNotificationCenter.defaultCenter postNotificationName:NSNotification.threadFriendRequestStatusChanged object:self.uniqueId]; + }]; } else { [self saveWithTransaction:transaction]; + [transaction.connection flushTransactionsWithCompletionQueue:dispatch_get_main_queue() completionBlock:^{ + [NSNotificationCenter.defaultCenter postNotificationName:NSNotification.threadFriendRequestStatusChanged object:self.uniqueId]; + }]; } - dispatch_async(dispatch_get_main_queue(), ^{ - [NSNotificationCenter.defaultCenter postNotificationName:@"threadFriendRequestStatusChanged" object:self.uniqueId]; - }); } - (BOOL)hasPendingFriendRequest diff --git a/SignalServiceKit/src/Loki/Extensions/Notification.swift b/SignalServiceKit/src/Loki/Extensions/Notification.swift index 1d50e82a2..35fffae9e 100644 --- a/SignalServiceKit/src/Loki/Extensions/Notification.swift +++ b/SignalServiceKit/src/Loki/Extensions/Notification.swift @@ -1,4 +1,8 @@ -extension Notification.Name { - static let threadFriendRequestStatusChanged = Notification.Name("threadFriendRequestStatusChanged") +public extension Notification.Name { + public static let threadFriendRequestStatusChanged = Notification.Name("threadFriendRequestStatusChanged") +} + +@objc public extension NSNotification { + @objc public static let threadFriendRequestStatusChanged = Notification.Name.threadFriendRequestStatusChanged.rawValue as NSString // Obj-C } diff --git a/SignalServiceKit/src/Loki/Extensions/SSKProtoPrekeyBundleMessage.swift b/SignalServiceKit/src/Loki/Extensions/SSKProtoPrekeyBundleMessage.swift index dedefc976..3c8572800 100644 --- a/SignalServiceKit/src/Loki/Extensions/SSKProtoPrekeyBundleMessage.swift +++ b/SignalServiceKit/src/Loki/Extensions/SSKProtoPrekeyBundleMessage.swift @@ -1,10 +1,6 @@ @objc public extension SSKProtoPrekeyBundleMessage { - private var accountManager: TSAccountManager { - return TSAccountManager.sharedInstance() - } - @objc public class func builder(fromPreKeyBundle preKeyBundle: PreKeyBundle) -> SSKProtoPrekeyBundleMessageBuilder { let builder = self.builder() @@ -19,7 +15,7 @@ } @objc public func createPreKeyBundle(withTransaction transaction: YapDatabaseReadWriteTransaction) -> PreKeyBundle? { - let registrationId = accountManager.getOrGenerateRegistrationId(transaction) + let registrationId = TSAccountManager.sharedInstance().getOrGenerateRegistrationId(transaction) return PreKeyBundle(registrationId: Int32(registrationId), deviceId: Int32(deviceID), preKeyId: Int32(prekeyID), diff --git a/SignalServiceKit/src/Loki/Messages/OWSEphemeralMessage.m b/SignalServiceKit/src/Loki/Messages/OWSEphemeralMessage.m index 4c4312f9c..d3bb6e7be 100644 --- a/SignalServiceKit/src/Loki/Messages/OWSEphemeralMessage.m +++ b/SignalServiceKit/src/Loki/Messages/OWSEphemeralMessage.m @@ -3,11 +3,11 @@ @implementation OWSEphemeralMessage -- (BOOL)shouldBeSaved { return NO; } - + (OWSEphemeralMessage *)createEmptyOutgoingMessageInThread:(TSThread *)thread { return [[OWSEphemeralMessage alloc] initOutgoingMessageWithTimestamp:NSDate.ows_millisecondTimeStamp inThread:thread messageBody:@"" attachmentIds:[NSMutableArray new] expiresInSeconds:0 expireStartedAt:0 isVoiceMessage:NO groupMetaMessage:TSGroupMetaMessageUnspecified quotedMessage:nil contactShare:nil linkPreview:nil]; } +- (BOOL)shouldBeSaved { return NO; } + @end