Reputation: 5896
I am using AFNetworking to do some data loading from an API in order to refresh a UITableView. I'd like for the refresh controller to reflect this as so.
User pulls down and refresh wheel appears
API call goes out (async call)
API call returns success or error
UITableViewController finishes refresh, reloads data, and refresh wheel disappears
So I add a refresh control in viewDidLoad of my custom UIViewController (my table view is a subview of this master view) like below:
//Set up refresh controller
tableController = [[UITableViewController alloc]init];
tableController.view = self.subscriptionsTable;
UIRefreshControl* refresh = [[UIRefreshControl alloc]init];
[refresh addTarget:self action:@selector(loadSubscriptions) forControlEvents:UIControlEventAllEvents];
tableController.refreshControl = refresh;
I then have my refresh method: (all the [connections asyncCall:self]
does is send an async call to the API via the AFNetworking pod, calling a completed method of my view controller when done)
-(void)loadSubscriptions{
updated = YES;
[connections asyncCall:self];
}
The view controllers completed methods looks like this
-(void)asyncDone(id)responseObject{
tableData = responseObject;
NSLog(@"Yay, I'm done!");
}
Unfortunately, the refresh control completes, of course, the minute the async call is sent, not when it finishes, leaving me with no updated data. How can I get that thing to hang out for a bit until the async call is actually completed?
Upvotes: 0
Views: 759
Reputation: 42325
You need to call beginRefreshing
and endRefreshing
when your async call starts and ends. You'll also need to store a reference to your refresh
control to do that.
So, in loadSubscriptions
and asyncDone:
:
-(void)loadSubscriptions{
[self.refreshControl beginRefreshing];
updated = YES;
[connections asyncCall:self];
}
-(void)asyncDone(id)responseObject{
tableData = responseObject;
NSLog(@"Yay, I'm done!");
[self.refreshControl endRefreshing];
}
Note: A lot of async methods will call their completion block on a background thread, so you may need to dispatch that endRefreshing
call to the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
[self.refreshControl endRefreshing];
});
Upvotes: 1