xcode 将消息从后台线程发送到 iOS 上的主线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12933397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:50:57  来源:igfitidea点击:

Sending messages from background thread to main thread on iOS

iphoneobjective-ciosxcodemultithreading

提问by Eric Brotto

I have an iOS app where I am completing tasks on the background thread. As each task is completed I want to send a message to the main thread so I can move along my custom progress bar to the appropriate stage.

我有一个 iOS 应用程序,我正在后台线程上完成任务。当每个任务完成时,我想向主线程发送一条消息,以便我可以沿着我的自定义进度条移动到适当的阶段。

What is the simplest, yet most secure way to achieve this?

实现这一目标的最简单但最安全的方法是什么?

EDIT 1

编辑 1

This is the method I am using:

这是我正在使用的方法:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
                                         (unsigned long)NULL), ^(void) {

     // get background tasks done. 
     [self.background backgroundMethod];

});

EDIT 2

编辑 2

Here is an example of what I would attempt using @Madboy's answer. Within the class where I perform methods in the background I have a pointer to my progress bar. Do we think this would work? (Please note the change also to EDIT 1).

这是我尝试使用@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. 
}

回答by Michael Ochs

This is based on what you are doing. Are you performing tasks in the background with GCD? Then you simply can call:

这是基于你在做什么。您是否使用 GCD 在后台执行任务?然后你只需调用:

dispatch_async(dispatch_get_main_queue(), ^{
    //your main thread task here
});

If you are running in an NSThread you could do this:

如果你在 NSThread 中运行,你可以这样做:

[myObject performSelectorOnMainThread:YOURSELECTOR withObject:YOUROBJECT waitUntilDone:NO];

回答by basar

You can use performSelectorOnMainThread:withObject:waitUntilDone:method of NSObject.

您可以使用NSObject 的performSelectorOnMainThread:withObject:waitUntilDone:方法。

The waitUntilDone parameter is a bool. You can pass YES to make your background thread wait until the method on main thread is done. You can pass NO to just fire that method and continue execution on background thread simultaneously with the main thread.

waitUntilDone 参数是一个布尔值。您可以通过 YES 使后台线程等待主线程上的方法完成。您可以传递 NO 以仅触发该方法并与主线程同时在后台线程上继续执行。

回答by Purpletoucan

NSNotificationCenterallows messaging between threads. You set up a 'listener' and can then broadcast notifications - the Apple documentation is very good, and it's easy to pick up.

NSNotificationCenter允许线程之间的消息传递。您设置了一个“侦听器”,然后可以广播通知 - Apple 文档非常好,而且很容易上手。