From 39c820b8666bd689abc6c2ad5a7367f0046121b6 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 2 Nov 2018 14:13:54 -0400 Subject: [PATCH] Distinguish 'app will/did become ready' events. --- Signal/src/AppDelegate.m | 8 -------- Signal/src/Jobs/SessionResetJob.swift | 9 +++++++++ SignalMessaging/contacts/OWSContactsManager.h | 4 ---- SignalMessaging/contacts/OWSContactsManager.m | 7 +++++++ .../src/Messages/OWSBatchMessageProcessor.h | 2 -- .../src/Messages/OWSBatchMessageProcessor.m | 11 ++++++----- SignalServiceKit/src/Messages/OWSMessageReceiver.h | 1 - SignalServiceKit/src/Messages/OWSMessageReceiver.m | 11 ++++++----- .../src/Network/MessageSenderJobQueue.swift | 9 +++++++++ SignalServiceKit/src/Util/AppReadiness.h | 13 +++++++------ SignalServiceKit/src/Util/AppReadiness.m | 6 ++---- SignalServiceKit/src/Util/JobQueue.swift | 12 ++++++++---- SignalServiceKit/src/Util/TypingIndicators.swift | 2 +- SignalShareExtension/ShareViewController.swift | 3 --- 14 files changed, 55 insertions(+), 43 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 56ed19a1a..414ea5ab8 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1130,17 +1130,9 @@ static NSTimeInterval launchStartedAt; [AppVersion.sharedInstance mainAppLaunchDidComplete]; - [Environment.shared.contactsManager setup]; - [Environment.shared.contactsManager startObserving]; - - // If there were any messages in our local queue which we hadn't yet processed. - [SSKEnvironment.shared.messageReceiver handleAnyUnprocessedEnvelopesAsync]; - [SSKEnvironment.shared.batchMessageProcessor handleAnyUnprocessedEnvelopesAsync]; [Environment.shared.audioSession setup]; [SSKEnvironment.shared.reachabilityManager setup]; - [SSKEnvironment.shared.messageSenderJobQueue setup]; - [AppEnvironment.shared.sessionResetJobQueue setup]; if (!Environment.shared.preferences.hasGeneratedThumbnails) { [self.primaryStorage.newDatabaseConnection diff --git a/Signal/src/Jobs/SessionResetJob.swift b/Signal/src/Jobs/SessionResetJob.swift index 7f68eaf5c..4fbac02b2 100644 --- a/Signal/src/Jobs/SessionResetJob.swift +++ b/Signal/src/Jobs/SessionResetJob.swift @@ -23,6 +23,15 @@ public class SessionResetJobQueue: NSObject, JobQueue { public let requiresInternet: Bool = true public var runningOperations: [SessionResetOperation] = [] + @objc + public override init() { + super.init() + + AppReadiness.runNowOrWhenAppWillBecomeReady { + self.setup() + } + } + @objc public func setup() { defaultSetup() diff --git a/SignalMessaging/contacts/OWSContactsManager.h b/SignalMessaging/contacts/OWSContactsManager.h index 4a6f2ed70..7e8ba009c 100644 --- a/SignalMessaging/contacts/OWSContactsManager.h +++ b/SignalMessaging/contacts/OWSContactsManager.h @@ -25,8 +25,6 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification; - (id)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage; -- (void)startObserving; - #pragma mark - Accessors @property (nonnull, readonly) ImageCache *avatarCache; @@ -44,8 +42,6 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification; - (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId; - (BOOL)hasSignalAccountForRecipientId:(NSString *)recipientId; -- (void)setup; - #pragma mark - System Contact Fetching // Must call `requestSystemContactsOnce` before accessing this method diff --git a/SignalMessaging/contacts/OWSContactsManager.m b/SignalMessaging/contacts/OWSContactsManager.m index 5a3158bc7..b1dba3516 100644 --- a/SignalMessaging/contacts/OWSContactsManager.m +++ b/SignalMessaging/contacts/OWSContactsManager.m @@ -82,6 +82,13 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan OWSSingletonAssert(); + [AppReadiness runNowOrWhenAppWillBecomeReady:^{ + [self setup]; + }]; + [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + [self startObserving]; + }]; + return self; } diff --git a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.h b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.h index 017dc21ab..c9664e5aa 100644 --- a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.h +++ b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.h @@ -24,8 +24,6 @@ NS_ASSUME_NONNULL_BEGIN plaintextData:(NSData *_Nullable)plaintextData transaction:(YapDatabaseReadWriteTransaction *)transaction; -- (void)handleAnyUnprocessedEnvelopesAsync; - @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m index ac9968280..54cd702c8 100644 --- a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m +++ b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m @@ -458,6 +458,12 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo _processingQueue = processingQueue; + [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (CurrentAppContext().isMainApp) { + [self.processingQueue drainQueue]; + } + }]; + return self; } @@ -475,11 +481,6 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo #pragma mark - instance methods -- (void)handleAnyUnprocessedEnvelopesAsync -{ - [self.processingQueue drainQueue]; -} - - (void)enqueueEnvelopeData:(NSData *)envelopeData plaintextData:(NSData *_Nullable)plaintextData transaction:(YapDatabaseReadWriteTransaction *)transaction diff --git a/SignalServiceKit/src/Messages/OWSMessageReceiver.h b/SignalServiceKit/src/Messages/OWSMessageReceiver.h index bff0571ba..b9c103090 100644 --- a/SignalServiceKit/src/Messages/OWSMessageReceiver.h +++ b/SignalServiceKit/src/Messages/OWSMessageReceiver.h @@ -20,7 +20,6 @@ NS_ASSUME_NONNULL_BEGIN + (void)asyncRegisterDatabaseExtension:(OWSStorage *)storage; - (void)handleReceivedEnvelopeData:(NSData *)envelopeData; -- (void)handleAnyUnprocessedEnvelopesAsync; @end diff --git a/SignalServiceKit/src/Messages/OWSMessageReceiver.m b/SignalServiceKit/src/Messages/OWSMessageReceiver.m index d9acc5ece..606f46fc7 100644 --- a/SignalServiceKit/src/Messages/OWSMessageReceiver.m +++ b/SignalServiceKit/src/Messages/OWSMessageReceiver.m @@ -414,6 +414,12 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin _processingQueue = processingQueue; + [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (CurrentAppContext().isMainApp) { + [self.processingQueue drainQueue]; + } + }]; + return self; } @@ -431,11 +437,6 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin #pragma mark - instance methods -- (void)handleAnyUnprocessedEnvelopesAsync -{ - [self.processingQueue drainQueue]; -} - - (void)handleReceivedEnvelopeData:(NSData *)envelopeData { if (envelopeData.length < 1) { diff --git a/SignalServiceKit/src/Network/MessageSenderJobQueue.swift b/SignalServiceKit/src/Network/MessageSenderJobQueue.swift index 755f6ecdb..62d8aa500 100644 --- a/SignalServiceKit/src/Network/MessageSenderJobQueue.swift +++ b/SignalServiceKit/src/Network/MessageSenderJobQueue.swift @@ -25,6 +25,15 @@ import Foundation @objc(SSKMessageSenderJobQueue) public class MessageSenderJobQueue: NSObject, JobQueue { + @objc + public override init() { + super.init() + + AppReadiness.runNowOrWhenAppWillBecomeReady { + self.setup() + } + } + // MARK: @objc(addMessage:transaction:) diff --git a/SignalServiceKit/src/Util/AppReadiness.h b/SignalServiceKit/src/Util/AppReadiness.h index 32ddecd70..f07ee3741 100755 --- a/SignalServiceKit/src/Util/AppReadiness.h +++ b/SignalServiceKit/src/Util/AppReadiness.h @@ -21,13 +21,14 @@ typedef void (^AppReadyBlock)(void); // // This method should only be called on the main thread. // The block will always be called on the main thread. -+ (void)runNowOrWhenAppWillBecomeReady:(AppReadyBlock)block NS_SWIFT_NAME(runNowOrWhenAppWillBecomeReady(_:)); - -// If the app is ready, the block is called immediately; -// otherwise it is called when the app becomes ready. // -// This method should only be called on the main thread. -// The block will always be called on the main thread. +// * The "will become ready" blocks are called before the "did become ready" blocks. +// * The "will become ready" blocks should be used for internal setup of components +// so that they are ready to interact with other components of the system. +// * The "did become ready" blocks should be used for any work that should be done +// on app launch, especially work that uses other components. +// * We should usually use "did become ready" blocks since they are safer. ++ (void)runNowOrWhenAppWillBecomeReady:(AppReadyBlock)block NS_SWIFT_NAME(runNowOrWhenAppWillBecomeReady(_:)); + (void)runNowOrWhenAppDidBecomeReady:(AppReadyBlock)block NS_SWIFT_NAME(runNowOrWhenAppDidBecomeReady(_:)); @end diff --git a/SignalServiceKit/src/Util/AppReadiness.m b/SignalServiceKit/src/Util/AppReadiness.m index efed97e16..fb90398d6 100755 --- a/SignalServiceKit/src/Util/AppReadiness.m +++ b/SignalServiceKit/src/Util/AppReadiness.m @@ -64,8 +64,7 @@ NS_ASSUME_NONNULL_BEGIN OWSAssertDebug(block); if (CurrentAppContext().isRunningTests) { - // We don't need to an any "on app ready" work - // in the tests. + // We don't need to do any "on app ready" work in the tests. return; } @@ -90,8 +89,7 @@ NS_ASSUME_NONNULL_BEGIN OWSAssertDebug(block); if (CurrentAppContext().isRunningTests) { - // We don't need to an any "on app ready" work - // in the tests. + // We don't need to do any "on app ready" work in the tests. return; } diff --git a/SignalServiceKit/src/Util/JobQueue.swift b/SignalServiceKit/src/Util/JobQueue.swift index 588688707..f773bccfd 100644 --- a/SignalServiceKit/src/Util/JobQueue.swift +++ b/SignalServiceKit/src/Util/JobQueue.swift @@ -88,7 +88,7 @@ public protocol JobQueue: DurableOperationDelegate { public extension JobQueue { - // MARK: Depenencies + // MARK: Dependencies var dbConnection: YapDatabaseConnection { return SSKEnvironment.shared.primaryStorage.dbReadWriteConnection @@ -109,7 +109,11 @@ public extension JobQueue { jobRecord.save(with: transaction) transaction.addCompletionQueue(.global()) { - self.workStep() + AppReadiness.runNowOrWhenAppDidBecomeReady { + DispatchQueue.global().async { + self.workStep() + } + } } } @@ -283,7 +287,7 @@ public class JobRecordFinder: NSObject, Finder { func allRecords(label: String, status: SSKJobRecordStatus, transaction: YapDatabaseReadTransaction) -> [SSKJobRecord] { var result: [SSKJobRecord] = [] - self.enumerateJobRecords(label: label, status: status, transaction: transaction) { jobRecord, stopPointer in + self.enumerateJobRecords(label: label, status: status, transaction: transaction) { jobRecord, _ in result.append(jobRecord) } return result @@ -293,7 +297,7 @@ public class JobRecordFinder: NSObject, Finder { let queryFormat = String(format: "WHERE %@ = ? AND %@ = ? ORDER BY %@", JobRecordField.status.rawValue, JobRecordField.label.rawValue, JobRecordField.sortId.rawValue) let query = YapDatabaseQuery(string: queryFormat, parameters: [status.rawValue, label]) - self.ext(transaction: transaction).enumerateKeysAndObjects(matching: query) { collection, key, object, stopPointer in + self.ext(transaction: transaction).enumerateKeysAndObjects(matching: query) { _, _, object, stopPointer in guard let jobRecord = object as? SSKJobRecord else { owsFailDebug("expecting jobRecord but found: \(object)") return diff --git a/SignalServiceKit/src/Util/TypingIndicators.swift b/SignalServiceKit/src/Util/TypingIndicators.swift index 898248d13..b608eb821 100644 --- a/SignalServiceKit/src/Util/TypingIndicators.swift +++ b/SignalServiceKit/src/Util/TypingIndicators.swift @@ -56,7 +56,7 @@ public class TypingIndicatorsImpl: NSObject, TypingIndicators { public override init() { super.init() - AppReadiness.runNowOrWhenAppIsReady { + AppReadiness.runNowOrWhenAppWillBecomeReady { self.setup() } } diff --git a/SignalShareExtension/ShareViewController.swift b/SignalShareExtension/ShareViewController.swift index d390825da..3d0581de0 100644 --- a/SignalShareExtension/ShareViewController.swift +++ b/SignalShareExtension/ShareViewController.swift @@ -262,9 +262,6 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed AppVersion.sharedInstance().saeLaunchDidComplete() - Environment.shared.contactsManager.setup() - Environment.shared.contactsManager.startObserving() - ensureRootViewController() // We don't need to use OWSMessageReceiver in the SAE.