Merge branch 'charlesmchen/offMainThread'

This commit is contained in:
Matthew Chen 2017-09-28 14:33:57 -04:00
commit b34076eeaf
7 changed files with 171 additions and 189 deletions

View file

@ -495,7 +495,7 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
// This will save the message.
[message updateWithCustomMessage:NSLocalizedString(@"GROUP_CREATED", nil)];
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (model.groupImage) {
NSData *data = UIImagePNGRepresentation(model.groupImage);
DataSource *_Nullable dataSource =

View file

@ -206,10 +206,10 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
#pragma mark - Signal Messages
- (void)notifyUserForErrorMessage:(TSErrorMessage *)message inThread:(TSThread *)thread {
OWSAssert([NSThread isMainThread]);
OWSAssert(message);
OWSAssert(thread);
dispatch_async(dispatch_get_main_queue(), ^{
if (thread.isMuted) {
return;
}
@ -245,6 +245,7 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
AudioServicesPlayAlertSound(_newMessageSound);
}
}
});
}
- (void)notifyUserForIncomingMessage:(TSIncomingMessage *)message
@ -255,6 +256,7 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
OWSAssert(thread);
OWSAssert(contactsManager);
dispatch_async(dispatch_get_main_queue(), ^{
if (thread.isMuted) {
return;
}
@ -290,8 +292,10 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
notification.category = (isNoLongerVerified ? Signal_Full_New_Message_Category_No_Longer_Verified
: Signal_Full_New_Message_Category);
notification.userInfo =
@{Signal_Thread_UserInfo_Key : thread.uniqueId, Signal_Message_UserInfo_Key : message.uniqueId};
notification.userInfo = @{
Signal_Thread_UserInfo_Key : thread.uniqueId,
Signal_Message_UserInfo_Key : message.uniqueId
};
if ([thread isGroupThread]) {
NSString *threadName = [NSString stringWithFormat:@"\"%@\"", groupName];
@ -312,8 +316,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
notification.alertBody = [NSString
stringWithFormat:@"%@ \"%@\"", NSLocalizedString(@"APN_MESSAGE_IN_GROUP", nil), groupName];
} else {
notification.alertBody =
[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"APN_MESSAGE_FROM", nil), senderName];
notification.alertBody = [NSString
stringWithFormat:@"%@ %@", NSLocalizedString(@"APN_MESSAGE_FROM", nil), senderName];
}
break;
}
@ -332,6 +336,7 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
AudioServicesPlayAlertSound(_newMessageSound);
}
}
});
}
- (BOOL)shouldPlaySoundForNotification

View file

@ -183,7 +183,7 @@ NSString *const kOWSBlockingManager_SyncedBlockedPhoneNumbersKey = @"kOWSBlockin
forKey:kOWSBlockingManager_BlockedPhoneNumbersKey
inCollection:kOWSBlockingManager_BlockedPhoneNumbersCollection];
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (sendSyncMessage) {
[self sendBlockedPhoneNumbersMessage:blockedPhoneNumbers];
} else {
@ -238,9 +238,7 @@ NSString *const kOWSBlockingManager_SyncedBlockedPhoneNumbersKey = @"kOWSBlockin
NSSet *syncedBlockedPhoneNumberSet = [[NSSet alloc] initWithArray:(syncedBlockedPhoneNumbers ?: [NSArray new])];
if (![_blockedPhoneNumberSet isEqualToSet:syncedBlockedPhoneNumberSet]) {
DDLogInfo(@"%@ retrying sync of blocked phone numbers", self.tag);
dispatch_async(dispatch_get_main_queue(), ^{
[self sendBlockedPhoneNumbersMessage:self.blockedPhoneNumbers];
});
}
}

View file

@ -495,11 +495,9 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
[messages addObject:message];
}
if (messages.count > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
for (OWSVerificationStateSyncMessage *message in messages) {
[self sendSyncVerificationStateMessage:message];
}
});
}
}
});
@ -509,7 +507,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
{
OWSAssert(message);
OWSAssert(message.verificationForRecipientId.length > 0);
OWSAssert([NSThread isMainThread]);
TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:message.verificationForRecipientId];
@ -519,7 +516,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
verificationStateSyncMessage:message];
[self.messageSender sendMessage:nullMessage
success:^{
dispatch_async(dispatch_get_main_queue(), ^{
DDLogInfo(@"%@ Successfully sent verification state NullMessage", self.tag);
[self.messageSender sendMessage:message
success:^{
@ -529,10 +525,8 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
[self clearSyncMessageForRecipientId:message.verificationForRecipientId];
}
failure:^(NSError *error) {
DDLogError(
@"%@ Failed to send verification state sync message with error: %@", self.tag, error);
DDLogError(@"%@ Failed to send verification state sync message with error: %@", self.tag, error);
}];
});
}
failure:^(NSError *_Nonnull error) {
DDLogError(@"%@ Failed to send verification state NullMessage with error: %@", self.tag, error);
@ -575,6 +569,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
}
NSData *identityKey = [rawIdentityKey removeKeyType];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
switch (verified.state) {
case OWSSignalServiceProtosVerifiedStateDefault:
[self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateDefault
@ -595,6 +590,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
return;
}
[self fireIdentityStateChangeNotification];
});
}
- (void)tryToApplyVerificationStateFromSyncMessage:(OWSVerificationState)verificationState

View file

@ -305,10 +305,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)notifyForErrorMessage:(TSErrorMessage *)errorMessage withEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
{
TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source];
dispatch_async(dispatch_get_main_queue(), ^{
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage
inThread:contactThread];
});
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage inThread:contactThread];
}
#pragma mark - Logging

View file

@ -604,9 +604,8 @@ NS_ASSUME_NONNULL_BEGIN
DDLogWarn(@"%@ ignoring unsupported sync request message", self.tag);
}
} else if (syncMessage.hasBlocked) {
// TODO: Do this synchronously.
dispatch_async(dispatch_get_main_queue(), ^{
NSArray<NSString *> *blockedPhoneNumbers = [syncMessage.blocked.numbers copy];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[_blockingManager setBlockedPhoneNumbers:blockedPhoneNumbers sendSyncMessage:NO];
});
} else if (syncMessage.read.count > 0) {
@ -616,10 +615,7 @@ NS_ASSUME_NONNULL_BEGIN
transaction:transaction];
} else if (syncMessage.hasVerified) {
DDLogInfo(@"%@ Received verification state for %@", self.tag, syncMessage.verified.destination);
// TODO: Do this synchronously.
dispatch_async(dispatch_get_main_queue(), ^{
[self.identityManager processIncomingSyncMessage:syncMessage.verified];
});
} else {
DDLogWarn(@"%@ Ignoring unsupported sync message.", self.tag);
}
@ -944,18 +940,15 @@ NS_ASSUME_NONNULL_BEGIN
[OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage
transaction:transaction];
// TODO: Do this synchronously.
dispatch_async(dispatch_get_main_queue(), ^{
[OWSDisappearingMessagesJob becomeConsistentWithConfigurationForMessage:incomingMessage
contactsManager:self.contactsManager];
// Update thread preview in inbox
[thread touch];
[thread touchWithTransaction:transaction];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage
inThread:thread
contactsManager:self.contactsManager];
});
}
return incomingMessage;

View file

@ -178,9 +178,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
object:nil];
// Try to start processing.
dispatch_async(dispatch_get_main_queue(), ^{
[self scheduleProcessing];
});
return self;
}
@ -198,7 +196,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
// Schedules a processing pass, unless one is already scheduled.
- (void)scheduleProcessing
{
DispatchMainThreadSafe(^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@synchronized(self)
{
if ([TSDatabaseView hasPendingViewRegistrations]) {
@ -241,7 +239,6 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
OWSReadReceiptsForLinkedDevicesMessage *message =
[[OWSReadReceiptsForLinkedDevicesMessage alloc] initWithReadReceipts:readReceiptsForLinkedDevices];
dispatch_async(dispatch_get_main_queue(), ^{
[self.messageSender sendMessage:message
success:^{
DDLogInfo(@"%@ Successfully sent %zd read receipt to linked devices.",
@ -251,7 +248,6 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
failure:^(NSError *error) {
DDLogError(@"%@ Failed to send read receipt to linked devices with error: %@", self.tag, error);
}];
});
}
NSArray<OWSReadReceipt *> *readReceiptsToSend = [self.toLinkedDevicesReadReceiptMap allValues];
@ -266,17 +262,14 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
[[OWSReadReceiptsForSenderMessage alloc] initWithThread:thread
messageTimestamps:timestamps.allObjects];
dispatch_async(dispatch_get_main_queue(), ^{
[self.messageSender sendMessage:message
success:^{
DDLogInfo(@"%@ Successfully sent %zd read receipts to sender.",
self.tag,
readReceiptsToSend.count);
DDLogInfo(
@"%@ Successfully sent %zd read receipts to sender.", self.tag, readReceiptsToSend.count);
}
failure:^(NSError *error) {
DDLogError(@"%@ Failed to send read receipts to sender with error: %@", self.tag, error);
}];
});
}
[self.toSenderReadReceiptMap removeAllObjects];
}