タイトルで述べたように、dispatch_async が 10 回起動しています。そして、GUI の応答性を高めるために使用します。しかし、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 を強制的に 1 回だけ起動するにはどうすればよいかということです。
助けていただければ幸いです、ありがとう
編集
これらの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];
}
}