我在UITableview中刷新数据时遇到问题。目前有一个UIRefreshControl,当触发(通过下拉)时,执行以下操作:
- (void)refreshByScrolling:(UIRefreshControl *)refreshControl {
[self refreshPriceInformation];
[refreshControl endRefreshing];
}
此时UI冻结(期望旋转refreshControl,直到[self refreshPriceInformation]方法完成。
无论如何,当我刷新数据时,UI是否会冻结?例如,在Twitter和Instagram应用程序上,您可以在开始向下滚动桌面后立即下拉刷新。
[self refreshPriceInformation]的代码位于
之下-(void)refreshPriceInformation
{
isRefreshing = true;
//Creating Queues for Concurrent refreshes
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_apply([watchedSkinList count], queue, ^(size_t i) {
SkinToWatch *watchedSkin =[watchedSkinList objectAtIndex:i];
[watchedSkin refreshPriceInformation];
});
dispatch_async(queue, ^{
NSLog(@"Saving Data to Phone");
// resaving data to phone
NSData *newdata = [NSKeyedArchiver archivedDataWithRootObject:watchedSkinList];
[[NSUserDefaults standardUserDefaults] setObject:newdata forKey:@"watchSkinsArray"];
[[NSUserDefaults standardUserDefaults]synchronize ];
isRefreshing = false;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
//Updating Refresh Label
[self updateDateAndSaveDurning:false];
});
}
谢谢!
EDIT ****** 正如HackerInHeels在下面所述,使用DISPATCH_QUEUE_PRIORITY_BACKGROUND包装dispatch_apply已经阻止了UI冻结!
跟进问题: 1)如何确保这是最后执行的代码块?
dispatch_async(queue, ^{
NSLog(@"Saving Data to Phone");
//rest of code...
});
在实施解决方案之前,它是最后一个执行的块。解决方案之后,情况就不再如此。我把它换成另一个队列吗?让它同步而不是aysnc?
再次感谢!
答案 0 :(得分:1)
你可以试试这个吗..
在dispatch_async
中包装SkinToWatch代码dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_apply([watchedSkinList count], queue, ^(size_t i) {
SkinToWatch *watchedSkin =[watchedSkinList objectAtIndex:i];
[watchedSkin refreshPriceInformation];
});
});
答案 1 :(得分:0)
试试这个
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
而不是
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];