我正在尝试使用确定的进度条完成长时间的计算。这适用于以下代码。但是,在调用 的方法中scan
,它立即转到完成更新对象scoreWithEquation
之前。我已经尝试了所有不同的方法来等待调度队列完成,但它们最终都会挂起 UI。我需要使用回调吗?我已经研究过这个解决方案,但我不明白如何来回传递所有参数和返回变量。 scan
StoredLibrary
-(void) scan{
if (!_sheet){
[NSBundle loadNibNamed: @"ProgressSheet" owner: self];
}
[NSApp beginSheet: _sheet
modalForWindow: [[NSApp delegate]window]
modalDelegate: self
didEndSelector: NULL
contextInfo: nil];
[_sheet makeKeyAndOrderFront:self];
[_progressBar setIndeterminate:YES];
[_progressBar setMinValue:0];
[_progressBar startAnimation:self];
__block NSMutableArray *tempTracks = [[NSMutableArray alloc]init];
//do stuff
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//do stuff
[_progressBar setDoubleValue:0.f];
[_progressBar setMaxValue:[listTracks count]];
[_progressBar setIndeterminate:NO];
for (iTunesTrack *listTrack in listTracks) {
//do stuff to tempTracks
}
dispatch_async(dispatch_get_main_queue(), ^{
[_progressBar incrementBy:1];
});
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[_progressBar setIndeterminate:YES];
[NSApp endSheet:self.sheet];
[self.sheet orderOut:self];
self.listTracks = tempTracks;
});
});
}
下面是调用的代码scan
:
- (IBAction)evaluate:(id)sender {
if (self.storedLibrary == nil){
self.storedLibrary = [[StoredLibrary alloc] init];
[self.storedLibrary scan];
}
self.tableTracks = [self.storedLibrary scoreWithequation:[_scoringEquation stringValue]
sortOrder:[_sortOrder titleOfSelectedItem]
trackLimit:[_trackLimit integerValue]];
}
我想保留scan
和scoreWithEquation
作为单独的方法,因为它们在其他地方被单独调用。