正如我在標題中提到的,我的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