I'm using UIRefreshControl
on my tableview to update items. And the end, I show a UIAlertController
to inform the user that the update was complete, and how many items were updated. Pretty straightforward, and it works fine for one thing. If I pull to refresh several times in a row, sometimes the refresh control is not dismissed, even after dismissing the alert. I need to swipe up the table to make it go away.
This is the code I use, all UI stuff is nicely done on the main thread:
if(refreshControl.refreshing) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self refreshItems];
dispatch_async(dispatch_get_main_queue(), ^{
[refreshControl endRefreshing];
[self.tableView reload];
[self showUpdateInfo];
});
});
}
Any idea what could cause this?
EDIT: This is how I create the refresh control in code (in viewDidLoad
):
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString: @"Checking for updates…"];
[refreshControl addTarget: self
action: @selector(refreshOutdatedItems)
forControlEvents: UIControlEventValueChanged];
self.refreshControl = refreshControl;
I believe it really has to do with the tableView not scrolling back up before the UIAlertController is presented. I tried to set the delay for showUpdateInfo method and that seems to worked. I guess when the user only pulls once it will take half a second to show the UIAlertController check if that helps. Here is my code
- (void)refreshOutdatedItems
{
NSLog(@"refresh");
if (self.refreshControl.refreshing) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (double i = 0 ; i < 100000000; i++){
};
dispatch_async(dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
[self performSelector:@selector(showUpdateInfo) withObject:nil afterDelay:0.5];
});
});
}
}
Let me know if helps.