From 7c84c4569abf5c32ce2b95b9a175e43609ff4ceb Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 17 Jun 2016 10:45:48 -0700 Subject: [PATCH] Only call UI modifying observers when UI is present (#1226) * centralize observers relevant to a "visible UI" * collocate init/teardown of observers // FREEBIE --- .../view controllers/MessagesViewController.m | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/Signal/src/view controllers/MessagesViewController.m b/Signal/src/view controllers/MessagesViewController.m index 6189dd872..70239473d 100644 --- a/Signal/src/view controllers/MessagesViewController.m +++ b/Signal/src/view controllers/MessagesViewController.m @@ -102,6 +102,10 @@ typedef enum : NSUInteger { @implementation MessagesViewController +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + - (void)peekSetup { _peek = YES; [self setComposeOnOpen:NO]; @@ -184,14 +188,34 @@ typedef enum : NSUInteger { self.senderId = ME_MESSAGE_IDENTIFIER; self.senderDisplayName = ME_MESSAGE_IDENTIFIER; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(startReadTimer) - name:UIApplicationWillEnterForegroundNotification - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(cancelReadTimer) - name:UIApplicationDidEnterBackgroundNotification - object:nil]; + +} + +- (void)toggleObservers:(BOOL)shouldObserve { + if (shouldObserve) { + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(yapDatabaseModified:) + name:YapDatabaseModifiedNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(startReadTimer) + name:UIApplicationWillEnterForegroundNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(cancelReadTimer) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; + } else { + [[NSNotificationCenter defaultCenter] removeObserver:self + name:YapDatabaseModifiedNotification + object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self + name:UIApplicationWillEnterForegroundNotification + object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self + name:UIApplicationDidEnterBackgroundNotification + object:nil]; + } } - (void)initializeTextView { @@ -203,6 +227,8 @@ typedef enum : NSUInteger { - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; + + [self toggleObservers:YES]; [self initializeToolbars]; NSInteger numberOfMessages = (NSInteger)[self.messageMappings numberOfItemsInGroup:self.thread.uniqueId]; @@ -257,6 +283,7 @@ typedef enum : NSUInteger { - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; + [self toggleObservers:NO]; if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) { // back button was pressed. @@ -1542,10 +1569,6 @@ typedef enum : NSUInteger { if (!_uiDatabaseConnection) { _uiDatabaseConnection = [[TSStorageManager sharedManager] newDatabaseConnection]; [_uiDatabaseConnection beginLongLivedReadTransaction]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(yapDatabaseModified:) - name:YapDatabaseModifiedNotification - object:nil]; } return _uiDatabaseConnection; } @@ -1961,10 +1984,6 @@ typedef enum : NSUInteger { } } -- (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} - #pragma mark 3D Touch Preview Actions - (NSArray> *)previewActionItems {