Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl: UITableView 'stuck' while refreshing

I'm having a problem implementing UIRefreshControl - in that when you pull down, the 'blob' works perfectly fine and the refresh spinner works fine, but the tableView doesn't scroll up to the spinner whilst refreshing. Instead, it stays where it was until the refreshing is complete, at which point it returns to the top of the screen

The code that does the refreshing is:

- (void)viewDidLoad {
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refreshView:)forControlEvents:UIControlEventValueChanged];
}

- (void)refreshView:(UIRefreshControl *)refresh  {
    dispatch_async(dispatch_get_main_queue(), ^{
        (...code to get new data here...)
        [self.refreshControl endRefreshing];
    }
}

I found that without dispatch_async, even the refresh spinner doesn't work - and the bit that was pulled down appears just white

Does anyone have any clues what I could be doing wrong? All implementation examples I've found seems to match what I'm doing, and I haven't found anything in the API docs that suggest I'm missing anything out

like image 296
thatdamnqa Avatar asked Nov 14 '12 21:11

thatdamnqa


1 Answers

You can change to following

- (void)refreshView:(UIRefreshControl *)refresh  {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // (...code to get new data here...)
        dispatch_async(dispatch_get_main_queue(), ^{
            //any UI refresh
            [self.refreshControl endRefreshing];
        });
    });
}

-refreshView: will get called on the main thread, and all UI updates are using the main thread. So if you use the main thread for "code to get new data" it will "stuck"

like image 131
Tomohisa Takaoka Avatar answered Sep 20 '22 14:09

Tomohisa Takaoka