diff --git a/SignalServiceKit/src/Storage/OWSPrimaryStorage.m b/SignalServiceKit/src/Storage/OWSPrimaryStorage.m index 9cfacba44..d21dcebea 100644 --- a/SignalServiceKit/src/Storage/OWSPrimaryStorage.m +++ b/SignalServiceKit/src/Storage/OWSPrimaryStorage.m @@ -30,50 +30,6 @@ NSString *const OWSUIDatabaseConnectionNotificationsKey = @"OWSUIDatabaseConnect NSString *const OWSPrimaryStorageExceptionName_CouldNotCreateDatabaseDirectory = @"TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory"; -void RunSyncRegistrationsForStorage(OWSStorage *storage) -{ - OWSCAssert(storage); - - // Synchronously register extensions which are essential for views. - [TSDatabaseView registerCrossProcessNotifier:storage]; -} - -void RunAsyncRegistrationsForStorage(OWSStorage *storage, dispatch_block_t completion) -{ - OWSCAssert(storage); - OWSCAssert(completion); - - // Asynchronously register other extensions. - // - // All sync registrations must be done before all async registrations, - // or the sync registrations will block on the async registrations. - - [TSDatabaseView asyncRegisterThreadInteractionsDatabaseView:storage]; - [TSDatabaseView asyncRegisterThreadDatabaseView:storage]; - [TSDatabaseView asyncRegisterUnreadDatabaseView:storage]; - [storage asyncRegisterExtension:[TSDatabaseSecondaryIndexes registerTimeStampIndex] - withName:[TSDatabaseSecondaryIndexes registerTimeStampIndexExtensionName]]; - [OWSMessageReceiver asyncRegisterDatabaseExtension:storage]; - [OWSBatchMessageProcessor asyncRegisterDatabaseExtension:storage]; - - [TSDatabaseView asyncRegisterUnseenDatabaseView:storage]; - [TSDatabaseView asyncRegisterThreadOutgoingMessagesDatabaseView:storage]; - [TSDatabaseView asyncRegisterThreadSpecialMessagesDatabaseView:storage]; - - [FullTextSearchFinder asyncRegisterDatabaseExtensionWithStorage:storage]; - [OWSIncomingMessageFinder asyncRegisterExtensionWithPrimaryStorage:storage]; - [TSDatabaseView asyncRegisterSecondaryDevicesDatabaseView:storage]; - [OWSDisappearingMessagesFinder asyncRegisterDatabaseExtensions:storage]; - [OWSFailedMessagesJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - [OWSIncompleteCallsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - [OWSFailedAttachmentDownloadsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - [OWSMediaGalleryFinder asyncRegisterDatabaseExtensionsWithPrimaryStorage:storage]; - - // NOTE: Always pass the completion to the _LAST_ of the async database - // view registrations. - [TSDatabaseView asyncRegisterLazyRestoreAttachmentsDatabaseView:storage completion:completion]; -} - void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) { OWSCAssert(storage); @@ -208,7 +164,8 @@ void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) - (void)runSyncRegistrations { - RunSyncRegistrationsForStorage(self); + // Synchronously register extensions which are essential for views. + [TSDatabaseView registerCrossProcessNotifier:self]; // See comments on OWSDatabaseConnection. // @@ -223,22 +180,48 @@ void VerifyRegistrationsForPrimaryStorage(OWSStorage *storage) - (void)runAsyncRegistrationsWithCompletion:(void (^_Nonnull)(void))completion { OWSAssert(completion); + OWSAssert(self.database); DDLogVerbose(@"%@ async registrations enqueuing.", self.logTag); - RunAsyncRegistrationsForStorage(self, ^{ - OWSAssertIsOnMainThread(); - - OWSAssert(!self.areAsyncRegistrationsComplete); - - DDLogVerbose(@"%@ async registrations complete.", self.logTag); - - self.areAsyncRegistrationsComplete = YES; - - completion(); - - [self verifyDatabaseViews]; - }); + // Asynchronously register other extensions. + // + // All sync registrations must be done before all async registrations, + // or the sync registrations will block on the async registrations. + [TSDatabaseView asyncRegisterThreadInteractionsDatabaseView:self]; + [TSDatabaseView asyncRegisterThreadDatabaseView:self]; + [TSDatabaseView asyncRegisterUnreadDatabaseView:self]; + [self asyncRegisterExtension:[TSDatabaseSecondaryIndexes registerTimeStampIndex] + withName:[TSDatabaseSecondaryIndexes registerTimeStampIndexExtensionName]]; + + [OWSMessageReceiver asyncRegisterDatabaseExtension:self]; + [OWSBatchMessageProcessor asyncRegisterDatabaseExtension:self]; + + [TSDatabaseView asyncRegisterUnseenDatabaseView:self]; + [TSDatabaseView asyncRegisterThreadOutgoingMessagesDatabaseView:self]; + [TSDatabaseView asyncRegisterThreadSpecialMessagesDatabaseView:self]; + + [FullTextSearchFinder asyncRegisterDatabaseExtensionWithStorage:self]; + [OWSIncomingMessageFinder asyncRegisterExtensionWithPrimaryStorage:self]; + [TSDatabaseView asyncRegisterSecondaryDevicesDatabaseView:self]; + [OWSDisappearingMessagesFinder asyncRegisterDatabaseExtensions:self]; + [OWSFailedMessagesJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [OWSIncompleteCallsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [OWSFailedAttachmentDownloadsJob asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [OWSMediaGalleryFinder asyncRegisterDatabaseExtensionsWithPrimaryStorage:self]; + [TSDatabaseView asyncRegisterLazyRestoreAttachmentsDatabaseView:self]; + + [self.database flushExtensionRequestsWithCompletionQueue:nil + completionBlock:^{ + OWSAssertIsOnMainThread(); + OWSAssert(!self.areAsyncRegistrationsComplete); + DDLogVerbose(@"%@ async registrations complete.", self.logTag); + self.areAsyncRegistrationsComplete = YES; + + completion(); + + [self verifyDatabaseViews]; + }]; } - (void)verifyDatabaseViews diff --git a/SignalServiceKit/src/Storage/TSDatabaseView.h b/SignalServiceKit/src/Storage/TSDatabaseView.h index 9954804a6..3e83d174b 100644 --- a/SignalServiceKit/src/Storage/TSDatabaseView.h +++ b/SignalServiceKit/src/Storage/TSDatabaseView.h @@ -58,7 +58,6 @@ extern NSString *const TSLazyRestoreAttachmentsDatabaseViewExtensionName; + (void)asyncRegisterSecondaryDevicesDatabaseView:(OWSStorage *)storage; -+ (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage - completion:(nullable dispatch_block_t)completion; ++ (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage; @end diff --git a/SignalServiceKit/src/Storage/TSDatabaseView.m b/SignalServiceKit/src/Storage/TSDatabaseView.m index 956ac1e12..8e0d1e0b2 100644 --- a/SignalServiceKit/src/Storage/TSDatabaseView.m +++ b/SignalServiceKit/src/Storage/TSDatabaseView.m @@ -362,7 +362,6 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup" } + (void)asyncRegisterLazyRestoreAttachmentsDatabaseView:(OWSStorage *)storage - completion:(nullable dispatch_block_t)completion { YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *_Nullable( YapDatabaseReadTransaction *transaction, NSString *collection, NSString *key, id object) { @@ -411,9 +410,7 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup" [[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[TSAttachment collection]]]; YapDatabaseView *view = [[YapDatabaseAutoView alloc] initWithGrouping:viewGrouping sorting:viewSorting versionTag:@"3" options:options]; - [storage asyncRegisterExtension:view - withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName - completion:completion]; + [storage asyncRegisterExtension:view withName:TSLazyRestoreAttachmentsDatabaseViewExtensionName]; } + (id)unseenDatabaseViewExtension:(YapDatabaseReadTransaction *)transaction