better handling of timers and notifications

This commit is contained in:
Sami Samhuri 2015-06-27 10:42:17 -07:00
parent a634a6d885
commit d823fe8aef

View file

@ -154,6 +154,8 @@ static const NSUInteger SectionPublished = 1;
} }
- (void)setupBlogStatusTimer { - (void)setupBlogStatusTimer {
// Just make sure everything is cleaned up in case we get called twice.
[self teardownBlogStatusTimer];
self.blogStatusTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateBlogStatus) userInfo:nil repeats:YES]; self.blogStatusTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateBlogStatus) userInfo:nil repeats:YES];
} }
@ -196,7 +198,6 @@ static const NSUInteger SectionPublished = 1;
- (void)viewWillAppear:(BOOL)animated { - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; [super viewWillAppear:animated];
[self setupBlogStatusTimer];
[self requestStatusWithCaching:YES]; [self requestStatusWithCaching:YES];
BOOL isPhone = [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone; BOOL isPhone = [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone;
if (isPhone && self.tableView.indexPathForSelectedRow) { if (isPhone && self.tableView.indexPathForSelectedRow) {
@ -205,21 +206,23 @@ static const NSUInteger SectionPublished = 1;
if (!self.postCollections) { if (!self.postCollections) {
[self requestPostsWithCaching:YES]; [self requestPostsWithCaching:YES];
} }
[self setupKeyboardNotifications];
} }
- (void)viewDidAppear:(BOOL)animated { - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; [super viewDidAppear:animated];
[self setupKeyboardNotifications];
[self setupBlogStatusTimer];
if (!self.hasAppeared) { if (!self.hasAppeared) {
self.hasAppeared = YES; self.hasAppeared = YES;
[self hideSearchBarAnimated:YES]; [self hideSearchBarAnimated:YES];
} }
} }
- (void)viewWillDisappear:(BOOL)animated { - (void)viewDidDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; [super viewWillDisappear:animated];
[self teardownBlogStatusTimer];
[self teardownKeyboardNotifications]; [self teardownKeyboardNotifications];
[self teardownBlogStatusTimer];
} }
- (void)viewDidLayoutSubviews { - (void)viewDidLayoutSubviews {
@ -239,7 +242,6 @@ static const NSUInteger SectionPublished = 1;
} }
- (PMKPromise *)requestStatusWithCaching:(BOOL)useCache { - (PMKPromise *)requestStatusWithCaching:(BOOL)useCache {
[self teardownBlogStatusTimer];
[self updateStatusLabel:@"Checking status" animated:YES]; [self updateStatusLabel:@"Checking status" animated:YES];
return [self.blogController requestBlogStatusWithCaching:useCache].then(^(BlogStatus *status) { return [self.blogController requestBlogStatusWithCaching:useCache].then(^(BlogStatus *status) {
self.blogStatusDate = status.date; self.blogStatusDate = status.date;
@ -249,7 +251,6 @@ static const NSUInteger SectionPublished = 1;
else { else {
self.blogStatusText = @"Everything published"; self.blogStatusText = @"Everything published";
} }
[self setupBlogStatusTimer];
[self updateBlogStatusAnimated:YES]; [self updateBlogStatusAnimated:YES];
return status; return status;
}).catch(^(NSError *error) { }).catch(^(NSError *error) {