diff --git a/Signal/src/util/OWSOrphanDataCleaner.m b/Signal/src/util/OWSOrphanDataCleaner.m index b89ad9499..afcf8069d 100644 --- a/Signal/src/util/OWSOrphanDataCleaner.m +++ b/Signal/src/util/OWSOrphanDataCleaner.m @@ -18,6 +18,7 @@ #import #import #import +#import #import NS_ASSUME_NONNULL_BEGIN diff --git a/SignalMessaging/ViewControllers/SharingThreadPickerViewController.m b/SignalMessaging/ViewControllers/SharingThreadPickerViewController.m index ec484e7d6..32c0a00bb 100644 --- a/SignalMessaging/ViewControllers/SharingThreadPickerViewController.m +++ b/SignalMessaging/ViewControllers/SharingThreadPickerViewController.m @@ -376,25 +376,22 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion); // TODO - in line with QuotedReply and other message attachments, saving should happen as part of sending // preparation rather than duplicated here and in the SAE - // TODO: <-------- - - [self.dbReadWriteConnection - asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { - if (contactShare.avatarImage) { - [contactShare.dbRecord saveAvatarImage:contactShare.avatarImage transaction:transaction]; - } + [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { + if (contactShare.avatarImage) { + [contactShare.dbRecord saveAvatarImage:contactShare.avatarImage transaction:transaction]; } - completionBlock:^{ - __block TSOutgoingMessage *outgoingMessage = nil; - outgoingMessage = [ThreadUtil sendMessageNonDurablyWithContactShare:contactShare.dbRecord - inThread:self.thread - messageSender:self.messageSender - completion:^(NSError *_Nullable error) { - sendCompletion(error, outgoingMessage); - }]; - // This is necessary to show progress. - self.outgoingMessage = outgoingMessage; - }]; + } + completion:^{ + __block TSOutgoingMessage *outgoingMessage = nil; + outgoingMessage = [ThreadUtil sendMessageNonDurablyWithContactShare:contactShare.dbRecord + inThread:self.thread + messageSender:self.messageSender + completion:^(NSError *_Nullable error) { + sendCompletion(error, outgoingMessage); + }]; + // This is necessary to show progress. + self.outgoingMessage = outgoingMessage; + }]; } diff --git a/SignalMessaging/environment/migrations/OWSDatabaseMigration.m b/SignalMessaging/environment/migrations/OWSDatabaseMigration.m index 27dcb3f15..04997bda7 100644 --- a/SignalMessaging/environment/migrations/OWSDatabaseMigration.m +++ b/SignalMessaging/environment/migrations/OWSDatabaseMigration.m @@ -72,14 +72,14 @@ NS_ASSUME_NONNULL_BEGIN OWSDatabaseConnection *dbConnection = (OWSDatabaseConnection *)self.primaryStorage.newDatabaseConnection; [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { - [self runUpWithTransaction:transaction]; - } - completionBlock:^{ - OWSLogInfo(@"Completed migration %@", self.uniqueId); - [self save]; - - completion(); - }]; + [self runUpWithTransaction:transaction]; + } + completion:^{ + OWSLogInfo(@"Completed migration %@", self.uniqueId); + [self save]; + + completion(); + }]; } #pragma mark - Database Connections diff --git a/SignalMessaging/environment/migrations/OWSResaveCollectionDBMigration.m b/SignalMessaging/environment/migrations/OWSResaveCollectionDBMigration.m index 796b68f35..ee0db59d3 100644 --- a/SignalMessaging/environment/migrations/OWSResaveCollectionDBMigration.m +++ b/SignalMessaging/environment/migrations/OWSResaveCollectionDBMigration.m @@ -25,14 +25,13 @@ NS_ASSUME_NONNULL_BEGIN [recordIds addObjectsFromArray:[transaction allKeysInCollection:collection]]; OWSLogInfo(@"Migrating %lu records from: %@.", (unsigned long)recordIds.count, collection); } - completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) - completionBlock:^{ - [self resaveBatch:recordIds - collection:collection - filter:filter - dbConnection:dbConnection - completion:completion]; - }]; + completion:^{ + [self resaveBatch:recordIds + collection:collection + filter:filter + dbConnection:dbConnection + completion:completion]; + }]; } - (void)resaveBatch:(NSMutableArray *)recordIds @@ -66,14 +65,14 @@ NS_ASSUME_NONNULL_BEGIN [entity saveWithTransaction:transaction]; } } - completionBlock:^{ - // Process the next batch. - [self resaveBatch:recordIds - collection:collection - filter:filter - dbConnection:dbConnection - completion:completion]; - }]; + completion:^{ + // Process the next batch. + [self resaveBatch:recordIds + collection:collection + filter:filter + dbConnection:dbConnection + completion:completion]; + }]; } @end diff --git a/SignalMessaging/profiles/OWSProfileManager.m b/SignalMessaging/profiles/OWSProfileManager.m index 6351c3ea7..d473b0ef2 100644 --- a/SignalMessaging/profiles/OWSProfileManager.m +++ b/SignalMessaging/profiles/OWSProfileManager.m @@ -31,6 +31,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN @@ -813,16 +814,16 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); [newRecipientIds addObject:recipientId]; } } - completionBlock:^{ - for (NSString *recipientId in newRecipientIds) { - [[NSNotificationCenter defaultCenter] - postNotificationNameAsync:kNSNotificationName_ProfileWhitelistDidChange - object:nil - userInfo:@{ - kNSNotificationKey_ProfileRecipientId : recipientId, - }]; - } - }]; + completion:^{ + for (NSString *recipientId in newRecipientIds) { + [[NSNotificationCenter defaultCenter] + postNotificationNameAsync:kNSNotificationName_ProfileWhitelistDidChange + object:nil + userInfo:@{ + kNSNotificationKey_ProfileRecipientId : recipientId, + }]; + } + }]; } - (BOOL)isUserInProfileWhitelist:(NSString *)recipientId @@ -859,16 +860,16 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); didChange = YES; } } - completionBlock:^{ - if (didChange) { - [[NSNotificationCenter defaultCenter] - postNotificationNameAsync:kNSNotificationName_ProfileWhitelistDidChange - object:nil - userInfo:@{ - kNSNotificationKey_ProfileGroupId : groupId, - }]; - } - }]; + completion:^{ + if (didChange) { + [[NSNotificationCenter defaultCenter] + postNotificationNameAsync:kNSNotificationName_ProfileWhitelistDidChange + object:nil + userInfo:@{ + kNSNotificationKey_ProfileGroupId : groupId, + }]; + } + }]; } - (void)addThreadToProfileWhitelist:(TSThread *)thread diff --git a/SignalMessaging/utils/ThreadUtil.m b/SignalMessaging/utils/ThreadUtil.m index ac720a1d7..6981efd44 100644 --- a/SignalMessaging/utils/ThreadUtil.m +++ b/SignalMessaging/utils/ThreadUtil.m @@ -26,6 +26,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN @@ -203,28 +204,23 @@ typedef void (^BuildOutgoingMessageCompletionBlock)(TSOutgoingMessage *savedMess block:^(void (^benchmarkCompletion)(void)) { // To avoid blocking the send flow, we dispatch an async write from within this read // transaction + [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull writeTransaction) { + [message saveWithTransaction:writeTransaction]; + + OWSLinkPreview *_Nullable linkPreview = + [self linkPreviewForLinkPreviewDraft:linkPreviewDraft + transaction:writeTransaction]; + if (linkPreview) { + [message updateWithLinkPreview:linkPreview transaction:writeTransaction]; + } - // TODO: <------- - - [self.dbConnection - asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull writeTransaction) { - [message saveWithTransaction:writeTransaction]; - - OWSLinkPreview *_Nullable linkPreview = - [self linkPreviewForLinkPreviewDraft:linkPreviewDraft - transaction:writeTransaction]; - if (linkPreview) { - [message updateWithLinkPreview:linkPreview transaction:writeTransaction]; - } - - NSMutableArray *attachmentInfos = [NSMutableArray new]; - for (SignalAttachment *attachment in attachments) { - OWSOutgoingAttachmentInfo *attachmentInfo = [attachment buildOutgoingAttachmentInfoWithMessage:message]; - [attachmentInfos addObject:attachmentInfo]; - } - completionBlock(message, attachmentInfos, writeTransaction); + NSMutableArray *attachmentInfos = [NSMutableArray new]; + for (SignalAttachment *attachment in attachments) { + OWSOutgoingAttachmentInfo *attachmentInfo = [attachment buildOutgoingAttachmentInfoWithMessage:message]; + [attachmentInfos addObject:attachmentInfo]; } - completionBlock:benchmarkCompletion]; + completionBlock(message, attachmentInfos, writeTransaction); + } completion:benchmarkCompletion]; }]; return message; @@ -721,7 +717,7 @@ typedef void (^BuildOutgoingMessageCompletionBlock)(TSOutgoingMessage *savedMess } @catch (NSException *exception) { // Do nothing } - }]; + } error:nil]; [TSAttachmentStream deleteAttachments]; } diff --git a/SignalServiceKit/src/Loki/Database/Storage.swift b/SignalServiceKit/src/Loki/Database/Storage.swift index cf6a5ad8c..6dcd71800 100644 --- a/SignalServiceKit/src/Loki/Database/Storage.swift +++ b/SignalServiceKit/src/Loki/Database/Storage.swift @@ -32,27 +32,39 @@ public final class Storage : NSObject { // • There can only be a single write transaction per database at any one time, so all write transactions must use `OWSPrimaryStorage`'s `dbReadWriteConnection`. // • Executing a write transaction from within a write transaction causes a deadlock and must be avoided. + @discardableResult @objc(writeWithBlock:) public static func objc_write(with block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> AnyPromise { return AnyPromise.from(write(with: block)) } + @discardableResult public static func write(with block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> Promise { + return write(with: block) { } + } + + @discardableResult + @objc(writeWithBlock:completion:) + public static func objc_write(with block: @escaping (YapDatabaseReadWriteTransaction) -> Void, completion: @escaping () -> Void) -> AnyPromise { + return AnyPromise.from(write(with: block, completion: completion)) + } + + @discardableResult + public static func write(with block: @escaping (YapDatabaseReadWriteTransaction) -> Void, completion: @escaping () -> Void) -> Promise { let (promise, seal) = Promise.pending() serialQueue.async { // TODO: There are cases where this isn't necessary owsStorage.dbReadWriteConnection.readWrite(block) + DispatchQueue.main.async { + completion() + } seal.fulfill(()) } return promise } /// Blocks the calling thread until the write has finished. + @discardableResult @objc(writeSyncWithBlock:error:) - public static func objc_writeSync(with block: @escaping (YapDatabaseReadWriteTransaction) -> Void) throws { - try writeSync(with: block) - } - - /// Blocks the calling thread until the write has finished. public static func writeSync(with block: @escaping (YapDatabaseReadWriteTransaction) -> Void) throws { try write(with: block).wait() } diff --git a/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m b/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m index ea845e659..d3c0794f6 100644 --- a/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m +++ b/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m @@ -17,6 +17,7 @@ #import "TSMessage.h" #import "TSThread.h" #import +#import NS_ASSUME_NONNULL_BEGIN diff --git a/SignalServiceKit/src/Messages/OWSFailedAttachmentDownloadsJob.m b/SignalServiceKit/src/Messages/OWSFailedAttachmentDownloadsJob.m index a26b7dfc0..202ee51a6 100644 --- a/SignalServiceKit/src/Messages/OWSFailedAttachmentDownloadsJob.m +++ b/SignalServiceKit/src/Messages/OWSFailedAttachmentDownloadsJob.m @@ -8,6 +8,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN diff --git a/SignalServiceKit/src/Messages/OWSFailedMessagesJob.m b/SignalServiceKit/src/Messages/OWSFailedMessagesJob.m index 678f0c93b..be2e799d9 100644 --- a/SignalServiceKit/src/Messages/OWSFailedMessagesJob.m +++ b/SignalServiceKit/src/Messages/OWSFailedMessagesJob.m @@ -9,6 +9,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN diff --git a/SignalServiceKit/src/Messages/OWSIncompleteCallsJob.m b/SignalServiceKit/src/Messages/OWSIncompleteCallsJob.m index 855076044..de35ef4a6 100644 --- a/SignalServiceKit/src/Messages/OWSIncompleteCallsJob.m +++ b/SignalServiceKit/src/Messages/OWSIncompleteCallsJob.m @@ -10,6 +10,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN diff --git a/SignalServiceKit/src/Storage/TSYapDatabaseObject.m b/SignalServiceKit/src/Storage/TSYapDatabaseObject.m index 897818e1f..013853160 100644 --- a/SignalServiceKit/src/Storage/TSYapDatabaseObject.m +++ b/SignalServiceKit/src/Storage/TSYapDatabaseObject.m @@ -6,6 +6,7 @@ #import "OWSPrimaryStorage.h" #import "SSKEnvironment.h" #import +#import NS_ASSUME_NONNULL_BEGIN @@ -52,11 +53,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)saveAsyncWithCompletionBlock:(void (^_Nullable)(void))completionBlock { - // TODO: <-------- - [[self dbReadWriteConnection] asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { + [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { [self saveWithTransaction:transaction]; - } - completionBlock:completionBlock]; + } completion:completionBlock]; } - (void)touchWithTransaction:(YapDatabaseReadWriteTransaction *)transaction diff --git a/SignalServiceKit/src/Util/OWSAnalytics.m b/SignalServiceKit/src/Util/OWSAnalytics.m index ac70dc735..28694f221 100755 --- a/SignalServiceKit/src/Util/OWSAnalytics.m +++ b/SignalServiceKit/src/Util/OWSAnalytics.m @@ -13,6 +13,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN