Distinguish 'app will/did become ready' events.

pull/1/head
Matthew Chen 7 years ago
parent eb2e16872e
commit 39c820b866

@ -1130,17 +1130,9 @@ static NSTimeInterval launchStartedAt;
[AppVersion.sharedInstance mainAppLaunchDidComplete]; [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]; [Environment.shared.audioSession setup];
[SSKEnvironment.shared.reachabilityManager setup]; [SSKEnvironment.shared.reachabilityManager setup];
[SSKEnvironment.shared.messageSenderJobQueue setup];
[AppEnvironment.shared.sessionResetJobQueue setup];
if (!Environment.shared.preferences.hasGeneratedThumbnails) { if (!Environment.shared.preferences.hasGeneratedThumbnails) {
[self.primaryStorage.newDatabaseConnection [self.primaryStorage.newDatabaseConnection

@ -23,6 +23,15 @@ public class SessionResetJobQueue: NSObject, JobQueue {
public let requiresInternet: Bool = true public let requiresInternet: Bool = true
public var runningOperations: [SessionResetOperation] = [] public var runningOperations: [SessionResetOperation] = []
@objc
public override init() {
super.init()
AppReadiness.runNowOrWhenAppWillBecomeReady {
self.setup()
}
}
@objc @objc
public func setup() { public func setup() {
defaultSetup() defaultSetup()

@ -25,8 +25,6 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification;
- (id)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage; - (id)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage;
- (void)startObserving;
#pragma mark - Accessors #pragma mark - Accessors
@property (nonnull, readonly) ImageCache *avatarCache; @property (nonnull, readonly) ImageCache *avatarCache;
@ -44,8 +42,6 @@ extern NSString *const OWSContactsManagerSignalAccountsDidChangeNotification;
- (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId; - (SignalAccount *)fetchOrBuildSignalAccountForRecipientId:(NSString *)recipientId;
- (BOOL)hasSignalAccountForRecipientId:(NSString *)recipientId; - (BOOL)hasSignalAccountForRecipientId:(NSString *)recipientId;
- (void)setup;
#pragma mark - System Contact Fetching #pragma mark - System Contact Fetching
// Must call `requestSystemContactsOnce` before accessing this method // Must call `requestSystemContactsOnce` before accessing this method

@ -82,6 +82,13 @@ NSString *const OWSContactsManagerKeyNextFullIntersectionDate = @"OWSContactsMan
OWSSingletonAssert(); OWSSingletonAssert();
[AppReadiness runNowOrWhenAppWillBecomeReady:^{
[self setup];
}];
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
[self startObserving];
}];
return self; return self;
} }

@ -24,8 +24,6 @@ NS_ASSUME_NONNULL_BEGIN
plaintextData:(NSData *_Nullable)plaintextData plaintextData:(NSData *_Nullable)plaintextData
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)handleAnyUnprocessedEnvelopesAsync;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -458,6 +458,12 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
_processingQueue = processingQueue; _processingQueue = processingQueue;
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (CurrentAppContext().isMainApp) {
[self.processingQueue drainQueue];
}
}];
return self; return self;
} }
@ -475,11 +481,6 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
#pragma mark - instance methods #pragma mark - instance methods
- (void)handleAnyUnprocessedEnvelopesAsync
{
[self.processingQueue drainQueue];
}
- (void)enqueueEnvelopeData:(NSData *)envelopeData - (void)enqueueEnvelopeData:(NSData *)envelopeData
plaintextData:(NSData *_Nullable)plaintextData plaintextData:(NSData *_Nullable)plaintextData
transaction:(YapDatabaseReadWriteTransaction *)transaction transaction:(YapDatabaseReadWriteTransaction *)transaction

@ -20,7 +20,6 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)asyncRegisterDatabaseExtension:(OWSStorage *)storage; + (void)asyncRegisterDatabaseExtension:(OWSStorage *)storage;
- (void)handleReceivedEnvelopeData:(NSData *)envelopeData; - (void)handleReceivedEnvelopeData:(NSData *)envelopeData;
- (void)handleAnyUnprocessedEnvelopesAsync;
@end @end

@ -414,6 +414,12 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin
_processingQueue = processingQueue; _processingQueue = processingQueue;
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (CurrentAppContext().isMainApp) {
[self.processingQueue drainQueue];
}
}];
return self; return self;
} }
@ -431,11 +437,6 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin
#pragma mark - instance methods #pragma mark - instance methods
- (void)handleAnyUnprocessedEnvelopesAsync
{
[self.processingQueue drainQueue];
}
- (void)handleReceivedEnvelopeData:(NSData *)envelopeData - (void)handleReceivedEnvelopeData:(NSData *)envelopeData
{ {
if (envelopeData.length < 1) { if (envelopeData.length < 1) {

@ -25,6 +25,15 @@ import Foundation
@objc(SSKMessageSenderJobQueue) @objc(SSKMessageSenderJobQueue)
public class MessageSenderJobQueue: NSObject, JobQueue { public class MessageSenderJobQueue: NSObject, JobQueue {
@objc
public override init() {
super.init()
AppReadiness.runNowOrWhenAppWillBecomeReady {
self.setup()
}
}
// MARK: // MARK:
@objc(addMessage:transaction:) @objc(addMessage:transaction:)

@ -21,13 +21,14 @@ typedef void (^AppReadyBlock)(void);
// //
// This method should only be called on the main thread. // This method should only be called on the main thread.
// The block will always 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 "will become ready" blocks are called before the "did become ready" blocks.
// The block will always be called on the main thread. // * 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(_:)); + (void)runNowOrWhenAppDidBecomeReady:(AppReadyBlock)block NS_SWIFT_NAME(runNowOrWhenAppDidBecomeReady(_:));
@end @end

@ -64,8 +64,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssertDebug(block); OWSAssertDebug(block);
if (CurrentAppContext().isRunningTests) { if (CurrentAppContext().isRunningTests) {
// We don't need to an any "on app ready" work // We don't need to do any "on app ready" work in the tests.
// in the tests.
return; return;
} }
@ -90,8 +89,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssertDebug(block); OWSAssertDebug(block);
if (CurrentAppContext().isRunningTests) { if (CurrentAppContext().isRunningTests) {
// We don't need to an any "on app ready" work // We don't need to do any "on app ready" work in the tests.
// in the tests.
return; return;
} }

@ -88,7 +88,7 @@ public protocol JobQueue: DurableOperationDelegate {
public extension JobQueue { public extension JobQueue {
// MARK: Depenencies // MARK: Dependencies
var dbConnection: YapDatabaseConnection { var dbConnection: YapDatabaseConnection {
return SSKEnvironment.shared.primaryStorage.dbReadWriteConnection return SSKEnvironment.shared.primaryStorage.dbReadWriteConnection
@ -109,7 +109,11 @@ public extension JobQueue {
jobRecord.save(with: transaction) jobRecord.save(with: transaction)
transaction.addCompletionQueue(.global()) { 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] { func allRecords(label: String, status: SSKJobRecordStatus, transaction: YapDatabaseReadTransaction) -> [SSKJobRecord] {
var result: [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) result.append(jobRecord)
} }
return result 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 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]) 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 { guard let jobRecord = object as? SSKJobRecord else {
owsFailDebug("expecting jobRecord but found: \(object)") owsFailDebug("expecting jobRecord but found: \(object)")
return return

@ -56,7 +56,7 @@ public class TypingIndicatorsImpl: NSObject, TypingIndicators {
public override init() { public override init() {
super.init() super.init()
AppReadiness.runNowOrWhenAppIsReady { AppReadiness.runNowOrWhenAppWillBecomeReady {
self.setup() self.setup()
} }
} }

@ -262,9 +262,6 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed
AppVersion.sharedInstance().saeLaunchDidComplete() AppVersion.sharedInstance().saeLaunchDidComplete()
Environment.shared.contactsManager.setup()
Environment.shared.contactsManager.startObserving()
ensureRootViewController() ensureRootViewController()
// We don't need to use OWSMessageReceiver in the SAE. // We don't need to use OWSMessageReceiver in the SAE.

Loading…
Cancel
Save