正如我在标题中提到的,我的dispatch_async正在解雇10次。我使用它来使GUI更加快速响应。但是当它发射10次时,要花很长时间才能完成所有必须做的事情。下面的代码:dispatch_async被解雇10次
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self calculateAllThatShizzle];
});
而且calculateAllThatShizzle方法有大约150只行计算(包括许多循环)。
我曾尝试以下操作:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self calculateAllThatShizzle];
});
但后来好像它只是整个生命周期中使用一次,我需要的页面显示每次启动它。
所以问题是:我如何强制dispatch_async只触发一次?
任何帮助,将不胜感激,谢谢
编辑
这些dispatch_async和dispatch_once在checkAndCalculateIfNecessary方法。而这种方法是从的PageControl喜欢那个叫:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1;
pageControl.currentPage = page;
DetailViewController *controller = [viewControllers objectAtIndex:page];
[controller saveTheValues];
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
if ((page + 1) == kNumberOfPages) {
[controller checkAndCalculateIfNeccessary];
}
}
调用dispatch_async方法在哪里? – danielbeard 2012-04-11 08:30:45
@danielbeard在PageControl滚动到页面时调用的方法 – Novarg 2012-04-11 08:32:18
- (void)scrollViewDidScroll:(UIScrollView *)scrollView? – danielbeard 2012-04-11 08:36:31