Search code examples
iphoneobjective-cuitableviewios7uirefreshcontrol

UIRefreshControl freezes when temporarily showing other tab (iOS 7)


I have the following on one of my tabs:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = YES;

    UIRefreshControl* refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.attributedTitle   = [[NSAttributedString alloc] initWithString:@"Sync"];
    [refreshControl addTarget:self 
                       action:@selector(refresh:) 
             forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;

    //### Workaround: http://stackoverflow.com/a/19126113/1971013
    dispatch_async(dispatch_get_main_queue(), ^
    {
        [self.refreshControl beginRefreshing];
        [self.refreshControl endRefreshing];
    });
}

- (void)refresh:(id)sender
{
    if ([Settings sharedSettings].haveAccount == YES)
    {
        [[DataManager sharedManager] synchronizeWithServer:^(NSError* error)
        {
            [sender endRefreshing];
        }];
    }
    else
    {
        [sender endRefreshing];
    }
}

Refresh control starts spinning normally when pulling down table.

However, when I, while it spins, shown an other tab shortly and then go back, the refresh control stops spinning.

Any idea why?


Solution

  • Try moving this piece of code from viewDidLoad to viewWillAppear:

    //### Workaround: http://stackoverflow.com/a/19126113/1971013
    dispatch_async(dispatch_get_main_queue(), ^
    {
        [self.refreshControl beginRefreshing];
        [self.refreshControl endRefreshing];
    });