More thread safety fixes.

// FREEBIE
This commit is contained in:
Matthew Chen 2017-09-28 21:12:02 -04:00
parent 8fbc996bca
commit c3dca21a69
6 changed files with 63 additions and 49 deletions

View File

@ -363,16 +363,20 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
hasCheckedContentLength = YES;
}
success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
if (![responseObject isKindOfClass:[NSData class]]) {
DDLogError(@"%@ Failed retrieval of attachment. Response had unexpected format.", self.tag);
NSError *error = OWSErrorMakeUnableToProcessServerResponseError();
return failureHandler(task, error);
}
successHandler((NSData *)responseObject);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (![responseObject isKindOfClass:[NSData class]]) {
DDLogError(@"%@ Failed retrieval of attachment. Response had unexpected format.", self.tag);
NSError *error = OWSErrorMakeUnableToProcessServerResponseError();
return failureHandler(task, error);
}
successHandler((NSData *)responseObject);
});
}
failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
DDLogError(@"Failed to retrieve attachment with error: %@", error.description);
return failureHandler(task, error);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DDLogError(@"Failed to retrieve attachment with error: %@", error.description);
return failureHandler(task, error);
});
}];
}

View File

@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
- (NSString *)description;
- (TSThread *)threadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
/**
* When an interaction is updated, it often affects the UI for it's containing thread. Touching it's thread will notify
* any observers so they can redraw any related UI.

View File

@ -77,6 +77,11 @@ NS_ASSUME_NONNULL_BEGIN
return [TSThread fetchObjectWithUniqueID:self.uniqueThreadId];
}
- (TSThread *)threadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
return [TSThread fetchObjectWithUniqueID:self.uniqueThreadId transaction:transaction];
}
- (void)touchThreadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
TSThread *thread = [TSThread fetchObjectWithUniqueID:self.uniqueThreadId transaction:transaction];

View File

@ -224,59 +224,62 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)becomeConsistentWithConfigurationForMessage:(TSMessage *)message
contactsManager:(id<ContactsManagerProtocol>)contactsManager
{
dispatch_async(self.serialQueue, ^{
[[self sharedJob] becomeConsistentWithConfigurationForMessage:message contactsManager:contactsManager];
});
}
- (void)becomeConsistentWithConfigurationForMessage:(TSMessage *)message
contactsManager:(id<ContactsManagerProtocol>)contactsManager
{
// Become eventually consistent in the case that the remote changed their settings at the same time.
// Also in case remote doesn't support expiring messages
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration =
[OWSDisappearingMessagesConfiguration fetchOrCreateDefaultWithThreadId:message.uniqueThreadId];
OWSAssert(message);
OWSAssert(contactsManager);
BOOL changed = NO;
if (message.expiresInSeconds == 0) {
if (disappearingMessagesConfiguration.isEnabled) {
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
// Become eventually consistent in the case that the remote changed their settings at the same time.
// Also in case remote doesn't support expiring messages
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration =
[OWSDisappearingMessagesConfiguration fetchOrCreateDefaultWithThreadId:message.uniqueThreadId];
BOOL changed = NO;
if (message.expiresInSeconds == 0) {
if (disappearingMessagesConfiguration.isEnabled) {
changed = YES;
DDLogWarn(@"%@ Received remote message which had no expiration set, disabling our expiration to become "
@"consistent.",
self.tag);
disappearingMessagesConfiguration.enabled = NO;
[disappearingMessagesConfiguration save];
}
} else if (message.expiresInSeconds != disappearingMessagesConfiguration.durationSeconds) {
changed = YES;
DDLogWarn(@"%@ Received remote message which had no expiration set, disabling our expiration to become "
DDLogInfo(@"%@ Received remote message with different expiration set, updating our expiration to become "
@"consistent.",
self.tag);
disappearingMessagesConfiguration.enabled = NO;
disappearingMessagesConfiguration.enabled = YES;
disappearingMessagesConfiguration.durationSeconds = message.expiresInSeconds;
[disappearingMessagesConfiguration save];
}
} else if (message.expiresInSeconds != disappearingMessagesConfiguration.durationSeconds) {
changed = YES;
DDLogInfo(
@"%@ Received remote message with different expiration set, updating our expiration to become consistent.",
self.tag);
disappearingMessagesConfiguration.enabled = YES;
disappearingMessagesConfiguration.durationSeconds = message.expiresInSeconds;
[disappearingMessagesConfiguration save];
}
if (!changed) {
return;
}
if (!changed) {
return;
}
if ([message isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)message;
NSString *contactName = [contactsManager displayNameForPhoneIdentifier:incomingMessage.messageAuthorId];
if ([message isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)message;
NSString *contactName = [contactsManager displayNameForPhoneIdentifier:incomingMessage.messageAuthorId];
// We want the info message to appear _before_ the message.
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp - 1
thread:message.thread
configuration:disappearingMessagesConfiguration
createdByRemoteName:contactName] save];
} else {
// We want the info message to appear _before_ the message.
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp - 1
thread:message.thread
configuration:disappearingMessagesConfiguration]
save];
}
// We want the info message to appear _before_ the message.
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp - 1
thread:message.thread
configuration:disappearingMessagesConfiguration
createdByRemoteName:contactName] save];
} else {
// We want the info message to appear _before_ the message.
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp - 1
thread:message.thread
configuration:disappearingMessagesConfiguration]
save];
}
});
}
- (void)startIfNecessary

View File

@ -600,9 +600,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
success:(void (^)())successHandler
failure:(RetryableFailureHandler)failureHandler
{
TSThread *thread = message.thread;
dispatch_async([OWSDispatch sendingQueue], ^{
TSThread *thread = message.thread;
if ([thread isKindOfClass:[TSGroupThread class]]) {
TSGroupThread *gThread = (TSGroupThread *)thread;

View File

@ -470,7 +470,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
// Use timestampForSorting which reflects local sort order, rather than timestamp
// which reflect sender time.
[self markAsReadBeforeTimestamp:message.timestampForSorting
thread:message.thread
thread:[message threadWithTransaction:transaction]
wasLocal:NO
transaction:transaction];
}