Don't use mainApplicationState in business logic.

This commit is contained in:
Matthew Chen 2018-01-12 14:24:35 -05:00
parent b271eab4c7
commit 2b528ad894
6 changed files with 29 additions and 13 deletions

View File

@ -120,6 +120,11 @@ NS_ASSUME_NONNULL_BEGIN
[[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle];
}
- (BOOL)isInBackground
{
return [UIApplication sharedApplication].applicationState == UIApplicationStateBackground;
}
- (UIApplicationState)mainApplicationState
{
return [UIApplication sharedApplication].applicationState;

View File

@ -64,8 +64,7 @@ NS_ASSUME_NONNULL_BEGIN
name:OWSApplicationDidEnterBackgroundNotification
object:nil];
self.shouldObserveDBModifications
= !(CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground);
self.shouldObserveDBModifications = !CurrentAppContext().isInBackground;
}
- (void)applicationWillEnterForeground:(NSNotification *)notification

View File

@ -401,12 +401,10 @@ public class SystemContactsFetcher: NSObject {
switch authorizationStatus {
case .notDetermined:
if CurrentAppContext().isMainApp {
if CurrentAppContext().mainApplicationState() == .background {
Logger.error("\(self.TAG) do not request contacts permission when app is in background")
completion(nil)
return
}
if CurrentAppContext().isInBackground() {
Logger.error("\(self.TAG) do not request contacts permission when app is in background")
completion(nil)
return
}
self.contactStoreAdapter.requestAccess { (granted, error) in
if let error = error {

View File

@ -531,8 +531,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
[DDLog flushLog];
if (CurrentAppContext().isMainApp) {
UIApplicationState applicationState = CurrentAppContext().mainApplicationState;
if (applicationState == UIApplicationStateBackground) {
if (CurrentAppContext().isInBackground) {
// TODO: Rather than crash here, we should detect the situation earlier
// and exit gracefully - (in the app delegate?). See the `
// This is a last ditch effort to avoid blowing away the user's database.
@ -572,8 +571,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass";
- (void)backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:(NSString *)errorDescription
{
OWSAssert(
CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground);
OWSAssert(CurrentAppContext().isMainApp && CurrentAppContext().isInBackground);
// Sleep to give analytics events time to be delivered.
[NSThread sleepForTimeInterval:5.0f];

View File

@ -34,9 +34,13 @@ typedef void (^BackgroundTaskExpirationHandler)(void);
// Should only be called if isMainApp is YES.
//
// In general, isMainAppAndActive will probably yield more readable code.
// Wherever possible, use isMainAppAndActive or isInBackground instead.
// This should only be used by debugging/logging code.
- (UIApplicationState)mainApplicationState;
// Similar to UIApplicationStateBackground, but works in SAE.
- (BOOL)isInBackground;
// Should start a background task if isMainApp is YES.
// Should just return UIBackgroundTaskInvalid if isMainApp is NO.
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:

View File

@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface ShareAppExtensionContext ()
@property (nonatomic) UIViewController *rootViewController;
@property (atomic) BOOL isSAEInBackground;
@end
@ -63,6 +64,8 @@ NS_ASSUME_NONNULL_BEGIN
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
self.isSAEInBackground = NO;
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidBecomeActiveNotification object:nil];
}
@ -83,6 +86,8 @@ NS_ASSUME_NONNULL_BEGIN
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
[DDLog flushLog];
self.isSAEInBackground = YES;
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidEnterBackgroundNotification object:nil];
}
@ -92,6 +97,8 @@ NS_ASSUME_NONNULL_BEGIN
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
self.isSAEInBackground = NO;
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillEnterForegroundNotification object:nil];
}
@ -121,6 +128,11 @@ NS_ASSUME_NONNULL_BEGIN
DDLogInfo(@"Ignoring request to set status bar style since we're in an app extension");
}
- (BOOL)isInBackground
{
return self.isSAEInBackground;
}
- (UIApplicationState)mainApplicationState
{
OWSFail(@"%@ called %s.", self.logTag, __PRETTY_FUNCTION__);