如果有关系的话:-我在使用故事板-核心数据- xcode 4.6
我的应用程序有一个特定视图的UITableViewController。在该视图中,如果用户单击一个按钮,该软件将经历几个过程,其中数据从Internet下载并保存到核心数据中。我确信这是一个资源占优势,这就是为什么我试图在单独的线程中完成这些进程。
注:-有一个操作顺序,因为腿依赖于交换。交易所取决于竞争和头寸。位置取决于比赛。否则,我会异步执行所有的操作。
问题:-这是我第一次与中央调度公司合作。我不确定我做得对。-如果我注释掉数据处理,UIProgressView是可见的,并按预期进行更新。随着数据处理的到位,系统似乎陷入困境,甚至无法显示UIProgressView。
下面是管理下载和进度的方法。
- (IBAction)downloadNow:(id)sender {
[progressView setHidden:NO];
[progressView setProgress:0.1 animated:YES];
dispatch_sync(backgroundQueue, ^(void){
[self saveRace];
[self updateProgress:0.2];
});
dispatch_sync(backgroundQueue, ^(void){
[self savePositions];
[self updateProgress:0.3];
});
dispatch_sync(backgroundQueue, ^(void){
[self downloadExchanges];
[self saveExchanges];
[self updateProgress:0.4];
});
dispatch_sync(backgroundQueue, ^(void){
[self downloadLegs];
[self saveLegs];
[self updateProgress:0.5];
});
dispatch_sync(backgroundQueue, ^(void){
Utilities *utilities = [[Utilities alloc] init];
[utilities calculateStartTimes:race with:managedObjectContext];
[self updateProgress:1.0];
});
}
(void)updateProgress:(double)completedPercentage {
if (completedPercentage == 1.0) {
[self goHome];
} else if ([importExchanges count] > 0) {
[progressView setProgress:completedPercentage animated:YES];
}
}
Any help is greatly appreciated.发布于 2013-06-23 14:49:35
方法dispatch_sync将阻止调用线程,我相信在您的情况下,这是主线程。因此,我认为最好将这些dispatch_sync块封装到一个dispatch_async块中。
例如:
dispatch_async(backgroundQueue, ^(void){
[self saveRace];
[self updateProgress:0.2];
[self savePositions];
[self updateProgress:0.3];
[self downloadExchanges];
[self saveExchanges];
[self updateProgress:0.4];
[self downloadLegs];
[self saveLegs];
[self updateProgress:0.5];
Utilities *utilities = [[Utilities alloc] init];
[utilities calculateStartTimes:race with:managedObjectContext];
[self updateProgress:1.0];
});之后,您可以将要在主线程中完成的progressView更新包装起来,因为它是UI更新。
- (void)updateProgress:(double)completedPercentage {
if (completedPercentage == 1.0) {
[self goHome];
} else if ([importExchanges count] > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[progressView setProgress:completedPercentage animated:YES];
});
}
}https://stackoverflow.com/questions/17245947
复制相似问题