Merge branch 'charlesmchen/offMainThread'

pull/1/head
Matthew Chen 8 years ago
commit b34076eeaf

@ -495,7 +495,7 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
// This will save the message. // This will save the message.
[message updateWithCustomMessage:NSLocalizedString(@"GROUP_CREATED", nil)]; [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) { if (model.groupImage) {
NSData *data = UIImagePNGRepresentation(model.groupImage); NSData *data = UIImagePNGRepresentation(model.groupImage);
DataSource *_Nullable dataSource = DataSource *_Nullable dataSource =

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

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

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

@ -305,10 +305,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)notifyForErrorMessage:(TSErrorMessage *)errorMessage withEnvelope:(OWSSignalServiceProtosEnvelope *)envelope - (void)notifyForErrorMessage:(TSErrorMessage *)errorMessage withEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
{ {
TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source]; 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 #pragma mark - Logging

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

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

Loading…
Cancel
Save