From 6f8f700ea02102621e88da6a6f96a869cdeefb7b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 20 Mar 2021 05:37:56 +0300 Subject: Fix The Bug No. 1 * do not run network requests when app is not active * handle situation when viewDidLoad happens before app is active --- Juick/ViewControllers/DialogsViewController.m | 64 ++++++++++++++++++++------- 1 file changed, 47 insertions(+), 17 deletions(-) (limited to 'Juick/ViewControllers/DialogsViewController.m') diff --git a/Juick/ViewControllers/DialogsViewController.m b/Juick/ViewControllers/DialogsViewController.m index 432cd53..cca5479 100644 --- a/Juick/ViewControllers/DialogsViewController.m +++ b/Juick/ViewControllers/DialogsViewController.m @@ -10,14 +10,25 @@ #import "ChatViewController.h" #import "ConversationCell.h" +@interface DialogsViewController() + +@property(nonatomic, assign) Boolean dataLoading; + +@property(nonatomic, assign) Boolean initialDataLoaded; + +-(void) applicationActivated; + +@end + @implementation DialogsViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor colorNamed:@"Background"]]; [self.tableView registerNib:[UINib nibWithNibName:@"ConversationCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"notificationCell"]; - [self refreshData]; self.refreshControl = [UIRefreshControl new]; [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationActivated) name:UIApplicationDidBecomeActiveNotification object:nil]; + [self refreshData]; } - (void)viewWillAppear:(BOOL)animated { @@ -29,23 +40,31 @@ - (void) refreshData { self.chats = [NSMutableArray array]; [self.tableView reloadData]; - [[AppDelegate shared].api fetchChats:^(NSArray *groups, NSError *err) { - if (err == nil) { - [self.chats addObjectsFromArray:groups]; - NSMutableArray *indexPaths = [NSMutableArray new]; - for (NSUInteger index = 0; index < [groups count]; index++) { - [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]]; + UIApplicationState state = [UIApplication sharedApplication].applicationState; + if (state == UIApplicationStateActive && self.dataLoading == NO) { + self.dataLoading = YES; + [[AppDelegate shared].api fetchChats:^(NSArray *groups, NSError *err) { + if (!self.initialDataLoaded) { + self.initialDataLoaded = YES; } - [self.tableView beginUpdates]; - [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; - [self.tableView endUpdates]; - self.tableView.backgroundView = [UIView new]; - } else { - [self setEmptyMessageView:@"Sign in to view chats"]; - [self.tableView reloadData]; - } - [self.refreshControl endRefreshing]; - }]; + self.dataLoading = NO; + if (err == nil) { + [self.chats addObjectsFromArray:groups]; + NSMutableArray *indexPaths = [NSMutableArray new]; + for (NSUInteger index = 0; index < [groups count]; index++) { + [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection: 0]]; + } + [self.tableView beginUpdates]; + [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES]; + [self.tableView endUpdates]; + self.tableView.backgroundView = [UIView new]; + } else { + [self setEmptyMessageView:@"Sign in to view chats"]; + [self.tableView reloadData]; + } + [self.refreshControl endRefreshing]; + }]; + } } #pragma mark - Table view data source @@ -96,5 +115,16 @@ [self.tableView setBackgroundView:messageLabel]; } +- (void)applicationActivated { + if (!self.initialDataLoaded) { + [self refreshData]; + } +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; +} + @end -- cgit v1.2.3