ios 如何在 UI 线程中运行代码从其他线程调用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12874917/
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-08-30 20:44:36  来源:igfitidea点击:

how to run code in the UI thread calling it from the others ones

ios

提问by pvllnspk

In Android there are several ways to run some code in the main thread from others ones:

在 Android 中,有几种方法可以在主线程中从其他线程中运行某些代码:

 1. Activity.runOnUiThread(Runnable r)
 2. new Handler.post(Runnable r);
 3. View.post

What are the analogues in iOS?

iOS中的类似物是什么?

 dispatch_async(dispatch_get_main_queue(), ^{

    });

Something else?

还有什么?

Thanks in advance.

提前致谢。

回答by AliSoftware

The preferred way nowadays is using GCD, with the code you quoted in your question:

现在首选的方法是使用 GCD,使用您在问题中引用的代码:

dispatch_async(dispatch_get_main_queue(), ^{
    // Your code to run on the main queue/thread
});

If you prefer using a more Object-Oriented approach than GCD, you may also use an NSOperation(like an NSBlockOperation) and add it to the [NSOperationQueue mainQueue].

如果您更喜欢使用比 GCD 更面向对象的方法,您也可以使用 an NSOperation(如 an NSBlockOperation)并将其添加到[NSOperationQueue mainQueue].

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    // Your code to run on the main queue/thread
 }];

This does quite the same thing as dispatch_async(dispatch_get_main_queue(), …), has the advantage of being more Objective-C/POO oriented that the plain C GCD function, but has the drawback of needing to allocate memory for creating the NSOperationobjects, whereas you can avoid it using plain C and GCD.

这与 完全相同dispatch_async(dispatch_get_main_queue(), …),具有比普通 C GCD 函数更面向 Objective-C/POO 的优点,但缺点是需要为创建NSOperation对象分配内存,而您可以使用普通 C 和GCD。



I recommend using GCD, but there are other ways, like those two which allow you to call a selector (method) on a given object from the main thread:

我建议使用 GCD,但还有其他方法,例如这两种方法,它们允许您从主线程调用给定对象上的选择器(方法):

  • - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait(method of NSObject so it can be called on any object)
  • Send - (void)performSelector:(SEL)aSelector target:(id)target argument:(id)anArgument order:(NSUInteger)order modes:(NSArray *)modeson the [NSRunLoop mainRunLoop]
  • - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait(NSObject 的方法,因此可以在任何对象上调用它)
  • 发送- (void)performSelector:(SEL)aSelector target:(id)target argument:(id)anArgument order:(NSUInteger)order modes:(NSArray *)modes[NSRunLoop mainRunLoop]

But those solutions are not as flexible as the GCD or NSOperationones, because they only let you call existing methods (so your object has to have a method that already exists and does what you want to perform), whereas GCD or -[NSOperationQueue addOperationWithBlock:]allows you to pass arbitrary code (using the block).

但是这些解决方案不如 GCD 或其他解决方案灵活NSOperation,因为它们只允许您调用现有方法(因此您的对象必须有一个已经存在的方法并执行您想要执行的操作),而 GCD 或-[NSOperationQueue addOperationWithBlock:]允许您任意传递代码(使用块)。