Distinguish 'app will/did become ready' events.

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

@ -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

@ -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()

@ -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

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

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

@ -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

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

@ -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) {

@ -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:)

@ -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

@ -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;
}

@ -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

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

@ -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.

Loading…
Cancel
Save