我有一个 iOS 应用程序,我正在其中完成后台线程上的任务。随着每个任务的完成,我想向主线程发送一条消息,这样我就可以沿着我的自定义进度条移动到适当的阶段。
实现这一目标的最简单但最安全的方法是什么?
编辑 1
这是我正在使用的方法:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
// get background tasks done.
[self.background backgroundMethod];
});
编辑 2
这是我尝试使用@Madboy 的答案的示例。在我在后台执行方法的类中,我有一个指向我的进度条的指针。我们认为这行得通吗?(请注意对 EDIT 1 的更改)。
@implementation BackgroundClass
@synthesize delegate;
@synthesize customProgressBar
- (void)backgroundMethod
{
// Run tasks
}
- (void)delegateSaysBackgroundMethodIsFinished
{
[self performSelectorOnMainThread:@selector(tasksDone:) withObject:self.customProgressBar waitUntilDone:NO];
}
@implementation CustomProgressBar
- (void)tasksDone
{
// change UI to reflect progress done.
}