From 85686d314408ab91dee0a73e94ef82e778764ed1 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 18 Dec 2017 21:34:22 -0500 Subject: [PATCH] Continue TSStorageManager refactor. --- Signal/src/AppDelegate.m | 6 +- .../AboutTableViewController.m | 2 +- .../ViewControllers/DebugUI/DebugUIMessages.m | 88 ++++++++----------- .../src/ViewControllers/DebugUI/DebugUIMisc.m | 2 +- .../ViewControllers/DebugUI/DebugUIStress.m | 18 ++-- .../ViewControllers/NewGroupViewController.m | 2 +- Signal/src/environment/SignalApp.m | 2 +- Signal/src/network/PushManager.m | 2 +- Signal/src/util/Pastelog.m | 4 +- SignalMessaging/Views/ContactsViewHelper.m | 2 +- .../contacts/SelectThreadViewController.m | 7 +- .../environment/VersionMigrations.m | 2 +- SignalMessaging/utils/OWSPreferences.h | 2 +- SignalMessaging/utils/OWSPreferences.m | 6 +- SignalServiceKit/src/Contacts/Contact.m | 2 +- .../src/Contacts/ContactsUpdater.m | 13 ++- SignalServiceKit/src/Contacts/TSThread.m | 4 +- SignalServiceKit/src/Storage/OWSStorage.h | 6 +- SignalServiceKit/src/Storage/OWSStorage.m | 37 ++++---- .../src/Storage/TSStorageManager.h | 3 +- .../src/Storage/TSStorageManager.m | 35 +++++++- 21 files changed, 130 insertions(+), 115 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 388b28385..61c0e0e96 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -219,8 +219,6 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; [TSStorageManager migrateToSharedData]; [OWSProfileManager migrateToSharedData]; [TSAttachmentStream migrateToSharedData]; - - [OWSPreferences setIsReadyForAppExtensions:YES]; } - (void)startupLogging @@ -764,6 +762,10 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; [OWSProfileManager.sharedManager fetchLocalUsersProfile]; [[OWSReadReceiptManager sharedManager] prepareCachedValues]; + + // Disable the SAE until the main app has successfully completed launch process + // at least once in the post-SAE world. + [OWSPreferences setIsReadyForAppExtensions]; } - (void)registrationStateDidChange diff --git a/Signal/src/ViewControllers/AboutTableViewController.m b/Signal/src/ViewControllers/AboutTableViewController.m index d2ebaf474..b9e15015a 100644 --- a/Signal/src/ViewControllers/AboutTableViewController.m +++ b/Signal/src/ViewControllers/AboutTableViewController.m @@ -71,7 +71,7 @@ #ifdef DEBUG __block NSUInteger threadCount; __block NSUInteger messageCount; - [TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [TSStorageManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { threadCount = [[transaction ext:TSThreadDatabaseViewExtensionName] numberOfItemsInAllGroups]; messageCount = [[transaction ext:TSMessageDatabaseViewExtensionName] numberOfItemsInAllGroups]; }]; diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 633a8cceb..6709fd7f4 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -694,8 +694,7 @@ NS_ASSUME_NONNULL_BEGIN NSMutableArray *result = [NSMutableArray new]; - [[TSStorageManager sharedManager].dbReadWriteConnection readWriteWithBlock:^( - YapDatabaseReadWriteTransaction *transaction) { + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { if ([thread isKindOfClass:[TSContactThread class]]) { TSContactThread *contactThread = (TSContactThread *)thread; @@ -836,12 +835,11 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(thread); NSArray *messages = [self unsavedSystemMessagesInThread:thread]; - [[TSStorageManager sharedManager].dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - for (TSInteraction *message in messages) { - [message saveWithTransaction:transaction]; - } - }]; + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + for (TSInteraction *message in messages) { + [message saveWithTransaction:transaction]; + } + }]; } + (void)createSystemMessageInThread:(TSThread *)thread @@ -850,10 +848,9 @@ NS_ASSUME_NONNULL_BEGIN NSArray *messages = [self unsavedSystemMessagesInThread:thread]; TSInteraction *message = messages[(NSUInteger)arc4random_uniform((uint32_t)messages.count)]; - [[TSStorageManager sharedManager].dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [message saveWithTransaction:transaction]; - }]; + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [message saveWithTransaction:transaction]; + }]; } + (void)sendTextAndSystemMessages:(int)counter thread:(TSThread *)thread @@ -912,8 +909,7 @@ NS_ASSUME_NONNULL_BEGIN + (void)createFakeUnreadMessages:(int)counter thread:(TSThread *)thread { - [TSStorageManager.sharedManager.dbReadWriteConnection readWriteWithBlock:^( - YapDatabaseReadWriteTransaction *transaction) { + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { for (int i = 0; i < counter; i++) { NSString *randomText = [self randomText]; TSIncomingMessage *message = [[TSIncomingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] @@ -947,10 +943,9 @@ NS_ASSUME_NONNULL_BEGIN + (void)sendFakeMessages:(NSUInteger)counter thread:(TSThread *)thread { - [TSStorageManager.sharedManager.dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [self sendFakeMessages:counter thread:thread transaction:transaction]; - }]; + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [self sendFakeMessages:counter thread:thread transaction:transaction]; + }]; } + (void)sendFakeMessages:(NSUInteger)counter @@ -1045,8 +1040,7 @@ NS_ASSUME_NONNULL_BEGIN return; } - [TSStorageManager.sharedManager.dbReadWriteConnection readWriteWithBlock:^( - YapDatabaseReadWriteTransaction *transaction) { + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { TSOutgoingMessage *message = [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] inThread:thread isVoiceMessage:NO @@ -1114,7 +1108,7 @@ NS_ASSUME_NONNULL_BEGIN [[TSGroupModel alloc] initWithTitle:groupName memberIds:recipientIds image:nil groupId:groupId]; __block TSGroupThread *thread; - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { thread = [TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction]; }]; @@ -1258,14 +1252,13 @@ NS_ASSUME_NONNULL_BEGIN [self resurrectNewOutgoingMessages2:messageCount thread:thread transaction:transaction]; }, ]; - [TSStorageManager.sharedManager.dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - int actionCount = 1 + (int)arc4random_uniform(3); - for (int actionIdx = 0; actionIdx < actionCount; actionIdx++) { - ActionBlock actionBlock = actionBlocks[(NSUInteger)arc4random_uniform((uint32_t)actionBlocks.count)]; - actionBlock(transaction); - } - }]; + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + int actionCount = 1 + (int)arc4random_uniform(3); + for (int actionIdx = 0; actionIdx < actionCount; actionIdx++) { + ActionBlock actionBlock = actionBlocks[(NSUInteger)arc4random_uniform((uint32_t)actionBlocks.count)]; + actionBlock(transaction); + } + }]; } + (void)deleteRandomMessages:(NSUInteger)count @@ -1414,15 +1407,14 @@ NS_ASSUME_NONNULL_BEGIN dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ DDLogInfo(@"%@ resurrectNewOutgoingMessages1.2: %zd", self.logTag, count); - [TSStorageManager.sharedManager.dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - for (TSOutgoingMessage *message in messages) { - [message removeWithTransaction:transaction]; - } - for (TSOutgoingMessage *message in messages) { - [message saveWithTransaction:transaction]; - } - }]; + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + for (TSOutgoingMessage *message in messages) { + [message removeWithTransaction:transaction]; + } + for (TSOutgoingMessage *message in messages) { + [message saveWithTransaction:transaction]; + } + }]; }); } @@ -1455,20 +1447,18 @@ NS_ASSUME_NONNULL_BEGIN dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ DDLogInfo(@"%@ resurrectNewOutgoingMessages2.2: %zd", self.logTag, count); - [TSStorageManager.sharedManager.dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + for (TSOutgoingMessage *message in messages) { + [message removeWithTransaction:transaction]; + } + }]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + DDLogInfo(@"%@ resurrectNewOutgoingMessages2.3: %zd", self.logTag, count); + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { for (TSOutgoingMessage *message in messages) { - [message removeWithTransaction:transaction]; + [message saveWithTransaction:transaction]; } }]; - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - DDLogInfo(@"%@ resurrectNewOutgoingMessages2.3: %zd", self.logTag, count); - [TSStorageManager.sharedManager.dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - for (TSOutgoingMessage *message in messages) { - [message saveWithTransaction:transaction]; - } - }]; }); }); } diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m index 74290ee75..29f0a0009 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m @@ -118,7 +118,7 @@ NS_ASSUME_NONNULL_BEGIN + (void)clearHasDismissedOffers { - [TSStorageManager.sharedManager.dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { NSMutableArray *contactThreads = [NSMutableArray new]; [transaction diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIStress.m b/Signal/src/ViewControllers/DebugUI/DebugUIStress.m index 25125c4fd..3f9903dcf 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIStress.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIStress.m @@ -475,15 +475,15 @@ NS_ASSUME_NONNULL_BEGIN + (void)hallucinateTwinGroup:(TSGroupThread *)groupThread { __block TSGroupThread *thread; - [[TSStorageManager sharedManager].dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { - TSGroupModel *groupModel = - [[TSGroupModel alloc] initWithTitle:[groupThread.groupModel.groupName stringByAppendingString:@" Copy"] - memberIds:[groupThread.groupModel.groupMemberIds mutableCopy] - image:groupThread.groupModel.groupImage - groupId:[SecurityUtils generateRandomBytes:16]]; - thread = [TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction]; - }]; + [TSStorageManager.dbReadWriteConnection + readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { + TSGroupModel *groupModel = + [[TSGroupModel alloc] initWithTitle:[groupThread.groupModel.groupName stringByAppendingString:@" Copy"] + memberIds:[groupThread.groupModel.groupMemberIds mutableCopy] + image:groupThread.groupModel.groupImage + groupId:[SecurityUtils generateRandomBytes:16]]; + thread = [TSGroupThread getOrCreateThreadWithGroupModel:groupModel transaction:transaction]; + }]; OWSAssert(thread); [SignalApp.sharedApp presentConversationForThread:thread]; diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index a478d77b5..3650901ce 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -446,7 +446,7 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68; TSGroupModel *model = [self makeGroup]; __block TSGroupThread *thread; - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { thread = [TSGroupThread getOrCreateThreadWithGroupModel:model transaction:transaction]; }]; diff --git a/Signal/src/environment/SignalApp.m b/Signal/src/environment/SignalApp.m index 76409b677..f6720d08c 100644 --- a/Signal/src/environment/SignalApp.m +++ b/Signal/src/environment/SignalApp.m @@ -172,7 +172,7 @@ DispatchMainThreadSafe(^{ __block TSThread *thread = nil; - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction]; }]; diff --git a/Signal/src/network/PushManager.m b/Signal/src/network/PushManager.m index bb5c04d72..97d616d0f 100644 --- a/Signal/src/network/PushManager.m +++ b/Signal/src/network/PushManager.m @@ -269,7 +269,7 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe NSString *threadId = userInfo[Signal_Thread_UserInfo_Key]; TSThread *thread = [TSThread fetchObjectWithUniqueID:threadId]; - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { // TODO: I suspect we only want to mark the message in // question as read. diff --git a/Signal/src/util/Pastelog.m b/Signal/src/util/Pastelog.m index fc5d55d35..37d452344 100644 --- a/Signal/src/util/Pastelog.m +++ b/Signal/src/util/Pastelog.m @@ -263,7 +263,7 @@ DispatchMainThreadSafe(^{ __block TSThread *thread = nil; - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction]; }]; @@ -283,7 +283,7 @@ DispatchMainThreadSafe(^{ __block TSThread *thread = nil; - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { thread = [[transaction ext:TSThreadDatabaseViewExtensionName] firstObjectInGroup:[TSThread collection]]; }]; diff --git a/SignalMessaging/Views/ContactsViewHelper.m b/SignalMessaging/Views/ContactsViewHelper.m index 5995d6957..ae58ff3d7 100644 --- a/SignalMessaging/Views/ContactsViewHelper.m +++ b/SignalMessaging/Views/ContactsViewHelper.m @@ -244,7 +244,7 @@ NS_ASSUME_NONNULL_BEGIN { if (!_nonSignalContacts) { NSMutableSet *nonSignalContacts = [NSMutableSet new]; - [[TSStorageManager sharedManager].dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [TSStorageManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { for (Contact *contact in self.contactsManager.allContactsMap.allValues) { NSArray *signalRecipients = [contact signalRecipientsWithTransaction:transaction]; if (signalRecipients.count < 1) { diff --git a/SignalMessaging/contacts/SelectThreadViewController.m b/SignalMessaging/contacts/SelectThreadViewController.m index 0542615f9..76990d1ca 100644 --- a/SignalMessaging/contacts/SelectThreadViewController.m +++ b/SignalMessaging/contacts/SelectThreadViewController.m @@ -229,10 +229,9 @@ NS_ASSUME_NONNULL_BEGIN } __block TSThread *thread = nil; - [[TSStorageManager sharedManager].dbReadWriteConnection - readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - thread = [TSContactThread getOrCreateThreadWithContactId:signalAccount.recipientId transaction:transaction]; - }]; + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + thread = [TSContactThread getOrCreateThreadWithContactId:signalAccount.recipientId transaction:transaction]; + }]; OWSAssert(thread); [self.selectThreadViewDelegate threadWasSelected:thread]; diff --git a/SignalMessaging/environment/VersionMigrations.m b/SignalMessaging/environment/VersionMigrations.m index c9b3a07e1..885eae8b7 100644 --- a/SignalMessaging/environment/VersionMigrations.m +++ b/SignalMessaging/environment/VersionMigrations.m @@ -180,7 +180,7 @@ NSError *deleteError; if ([fm removeItemAtPath:bloomFilterPath error:&deleteError]) { DDLogInfo(@"Successfully removed bloom filter cache."); - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { [transaction removeAllObjectsInCollection:@"TSRecipient"]; }]; diff --git a/SignalMessaging/utils/OWSPreferences.h b/SignalMessaging/utils/OWSPreferences.h index 12a7ca83a..52cc7169c 100644 --- a/SignalMessaging/utils/OWSPreferences.h +++ b/SignalMessaging/utils/OWSPreferences.h @@ -28,7 +28,7 @@ extern NSString *const OWSPreferencesKeyEnableDebugLog; #pragma mark - Specific Preferences + (BOOL)isReadyForAppExtensions; -+ (void)setIsReadyForAppExtensions:(BOOL)value; ++ (void)setIsReadyForAppExtensions; // TSAccountManager is the source of truth; this is less reliable // and should only be used in edge cases where the database is not diff --git a/SignalMessaging/utils/OWSPreferences.m b/SignalMessaging/utils/OWSPreferences.m index 31283ae15..9580abe39 100644 --- a/SignalMessaging/utils/OWSPreferences.m +++ b/SignalMessaging/utils/OWSPreferences.m @@ -23,7 +23,7 @@ NSString *const OWSPreferencesKeyCallKitPrivacyEnabled = @"CallKitPrivacyEnabled NSString *const OWSPreferencesKeyCallsHideIPAddress = @"CallsHideIPAddress"; NSString *const OWSPreferencesKeyHasDeclinedNoContactsView = @"hasDeclinedNoContactsView"; NSString *const OWSPreferencesKeyIOSUpgradeNagVersion = @"iOSUpgradeNagVersion"; -NSString *const OWSPreferencesKey_IsReadyForAppExtensions = @"isReadyForAppExtensions"; +NSString *const OWSPreferencesKey_IsReadyForAppExtensions = @"isReadyForAppExtensions2"; NSString *const OWSPreferencesKey_IsRegistered = @"OWSPreferencesKey_IsRegistered"; @implementation OWSPreferences @@ -75,9 +75,9 @@ NSString *const OWSPreferencesKey_IsRegistered = @"OWSPreferencesKey_IsRegistere } } -+ (void)setIsReadyForAppExtensions:(BOOL)value ++ (void)setIsReadyForAppExtensions { - [NSUserDefaults.appUserDefaults setObject:@(value) forKey:OWSPreferencesKey_IsReadyForAppExtensions]; + [NSUserDefaults.appUserDefaults setObject:@(YES) forKey:OWSPreferencesKey_IsReadyForAppExtensions]; [NSUserDefaults.appUserDefaults synchronize]; } diff --git a/SignalServiceKit/src/Contacts/Contact.m b/SignalServiceKit/src/Contacts/Contact.m index c0c291c16..69c6ca0a8 100644 --- a/SignalServiceKit/src/Contacts/Contact.m +++ b/SignalServiceKit/src/Contacts/Contact.m @@ -268,7 +268,7 @@ NS_ASSUME_NONNULL_BEGIN - (NSArray *)textSecureIdentifiers { __block NSMutableArray *identifiers = [NSMutableArray array]; - [[TSStorageManager sharedManager].dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [TSStorageManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { for (PhoneNumber *number in self.parsedPhoneNumbers) { if ([SignalRecipient recipientWithTextSecureIdentifier:number.toE164 withTransaction:transaction]) { [identifiers addObject:number.toE164]; diff --git a/SignalServiceKit/src/Contacts/ContactsUpdater.m b/SignalServiceKit/src/Contacts/ContactsUpdater.m index 09b7d6837..6ef8c92ee 100644 --- a/SignalServiceKit/src/Contacts/ContactsUpdater.m +++ b/SignalServiceKit/src/Contacts/ContactsUpdater.m @@ -122,11 +122,10 @@ NS_ASSUME_NONNULL_BEGIN } NSMutableSet *recipientIds = [NSMutableSet set]; - [[TSStorageManager sharedManager].dbReadConnection - readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { - NSArray *allRecipientKeys = [transaction allKeysInCollection:[SignalRecipient collection]]; - [recipientIds addObjectsFromArray:allRecipientKeys]; - }]; + [TSStorageManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { + NSArray *allRecipientKeys = [transaction allKeysInCollection:[SignalRecipient collection]]; + [recipientIds addObjectsFromArray:allRecipientKeys]; + }]; NSMutableSet *allContacts = [[abPhoneNumbers setByAddingObjectsFromSet:recipientIds] mutableCopy]; @@ -135,7 +134,7 @@ NS_ASSUME_NONNULL_BEGIN [recipientIds minusSet:matchedIds]; // Cleaning up unregistered identifiers - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { for (NSString *identifier in recipientIds) { SignalRecipient *recipient = @@ -185,7 +184,7 @@ NS_ASSUME_NONNULL_BEGIN } // Insert or update contact attributes - [[TSStorageManager sharedManager].dbReadWriteConnection + [TSStorageManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { for (NSString *identifier in attributesForIdentifier) { SignalRecipient *recipient = [SignalRecipient recipientWithTextSecureIdentifier:identifier diff --git a/SignalServiceKit/src/Contacts/TSThread.m b/SignalServiceKit/src/Contacts/TSThread.m index b905655c7..2bc4d720e 100644 --- a/SignalServiceKit/src/Contacts/TSThread.m +++ b/SignalServiceKit/src/Contacts/TSThread.m @@ -246,7 +246,7 @@ NS_ASSUME_NONNULL_BEGIN - (TSInteraction *) lastInteraction { __block TSInteraction *last; - [TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [TSStorageManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { last = [[transaction ext:TSMessageDatabaseViewExtensionName] lastObjectInGroup:self.uniqueId]; }]; return last; @@ -255,7 +255,7 @@ NS_ASSUME_NONNULL_BEGIN - (TSInteraction *)lastInteractionForInbox { __block TSInteraction *last = nil; - [TSStorageManager.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + [TSStorageManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [[transaction ext:TSMessageDatabaseViewExtensionName] enumerateRowsInGroup:self.uniqueId withOptions:NSEnumerationReverse diff --git a/SignalServiceKit/src/Storage/OWSStorage.h b/SignalServiceKit/src/Storage/OWSStorage.h index a9b499904..fcfd00a51 100644 --- a/SignalServiceKit/src/Storage/OWSStorage.h +++ b/SignalServiceKit/src/Storage/OWSStorage.h @@ -20,16 +20,14 @@ NS_ASSUME_NONNULL_BEGIN // TODO: Deprecate? - (nullable YapDatabaseConnection *)newDatabaseConnection; -// TODO: Deprecate. -@property (nullable, nonatomic, readonly) YapDatabaseConnection *dbReadConnection; -@property (nullable, nonatomic, readonly) YapDatabaseConnection *dbReadWriteConnection; - - (BOOL)registerExtension:(YapDatabaseExtension *)extension withName:(NSString *)extensionName; - (void)asyncRegisterExtension:(YapDatabaseExtension *)extension withName:(NSString *)extensionName completionBlock:(nullable void (^)(BOOL ready))completionBlock; - (nullable id)registeredExtension:(NSString *)extensionName; +- (void)resetStorage; + #pragma mark - Password /** diff --git a/SignalServiceKit/src/Storage/OWSStorage.m b/SignalServiceKit/src/Storage/OWSStorage.m index f90092062..a2832550e 100644 --- a/SignalServiceKit/src/Storage/OWSStorage.m +++ b/SignalServiceKit/src/Storage/OWSStorage.m @@ -243,27 +243,27 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass"; { self = [super init]; - if (![self tryToLoadDatabase]) { - // Failing to load the database is catastrophic. - // - // The best we can try to do is to discard the current database - // and behave like a clean install. - OWSProdCritical([OWSAnalyticsEvents storageErrorCouldNotLoadDatabase]); + if (self) { + if (![self tryToLoadDatabase]) { + // Failing to load the database is catastrophic. + // + // The best we can try to do is to discard the current database + // and behave like a clean install. + OWSProdCritical([OWSAnalyticsEvents storageErrorCouldNotLoadDatabase]); - // Try to reset app by deleting database. - // Disabled resetting storage until we have better data on why this happens. - // [self resetAllStorage]; + // Try to reset app by deleting database. + // Disabled resetting storage until we have better data on why this happens. + // [self resetAllStorage]; - if (![self tryToLoadDatabase]) { - OWSProdCritical([OWSAnalyticsEvents storageErrorCouldNotLoadDatabaseSecondAttempt]); + if (![self tryToLoadDatabase]) { + OWSProdCritical([OWSAnalyticsEvents storageErrorCouldNotLoadDatabaseSecondAttempt]); - // Sleep to give analytics events time to be delivered. - [NSThread sleepForTimeInterval:15.0f]; + // Sleep to give analytics events time to be delivered. + [NSThread sleepForTimeInterval:15.0f]; - [NSException raise:OWSStorageExceptionName_NoDatabase format:@"Failed to initialize database."]; + [NSException raise:OWSStorageExceptionName_NoDatabase format:@"Failed to initialize database."]; + } } - - OWSSingletonAssert(); } return self; @@ -302,8 +302,6 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass"; } _database = database; - _dbReadConnection = self.newDatabaseConnection; - _dbReadWriteConnection = self.newDatabaseConnection; return YES; } @@ -369,9 +367,6 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass"; { self.database = nil; - _dbReadConnection = nil; - _dbReadWriteConnection = nil; - [self deleteDatabaseFile]; } diff --git a/SignalServiceKit/src/Storage/TSStorageManager.h b/SignalServiceKit/src/Storage/TSStorageManager.h index 81a567d31..570ac6d69 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager.h +++ b/SignalServiceKit/src/Storage/TSStorageManager.h @@ -28,7 +28,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)setupDatabaseWithSafeBlockingMigrations:(void (^_Nonnull)(void))safeBlockingMigrationsBlock; -// TODO: Deprecate. +- (YapDatabaseConnection *)dbReadConnection; +- (YapDatabaseConnection *)dbReadWriteConnection; + (YapDatabaseConnection *)dbReadConnection; + (YapDatabaseConnection *)dbReadWriteConnection; diff --git a/SignalServiceKit/src/Storage/TSStorageManager.m b/SignalServiceKit/src/Storage/TSStorageManager.m index 310735454..a389cd39d 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager.m +++ b/SignalServiceKit/src/Storage/TSStorageManager.m @@ -28,6 +28,15 @@ NSString *const TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory #pragma mark - +@interface TSStorageManager () + +@property (nonatomic, readonly) YapDatabaseConnection *dbReadConnection; +@property (nonatomic, readonly) YapDatabaseConnection *dbReadWriteConnection; + +@end + +#pragma mark - + @implementation TSStorageManager + (instancetype)sharedManager { @@ -43,6 +52,28 @@ NSString *const TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory return sharedManager; } +- (instancetype)initStorage +{ + self = [super initStorage]; + + if (self) { + _dbReadConnection = self.newDatabaseConnection; + _dbReadWriteConnection = self.newDatabaseConnection; + + OWSSingletonAssert(); + } + + return self; +} + +- (void)resetStorage +{ + _dbReadConnection = nil; + _dbReadWriteConnection = nil; + + [super resetStorage]; +} + - (void)setupDatabaseWithSafeBlockingMigrations:(void (^_Nonnull)(void))safeBlockingMigrationsBlock { // Synchronously register extensions which are essential for views. @@ -193,12 +224,12 @@ NSString *const TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory + (YapDatabaseConnection *)dbReadConnection { - return TSStorageManager.sharedManager.dbReadConnection; + return TSStorageManager.dbReadConnection; } + (YapDatabaseConnection *)dbReadWriteConnection { - return TSStorageManager.sharedManager.dbReadWriteConnection; + return TSStorageManager.dbReadWriteConnection; } - (void)deleteDatabaseFile