performSelectorOnMainThread

performSelectorOnMainThread

本文介绍了在主线程使用dispatch_async或performSelectorOnMainThread执行UI的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  Grand中央调度(GCD)与performSelector - 需要

要在主线程上执行的东西,我应该使用 dispatch_async performSelectorOnMainThread ?是否有一个preferred方式吧/或错误,和/或最佳做法?

To execute "stuff" on the main thread, should I use dispatch_async or performSelectorOnMainThread? Is there a preferred way, right/or wrong, and/or best practice?

例如:的URLRequest 方法:我是 NSURLConnection的sendAsynchronousRequest块内执行一些逻辑。因为我做的东西到主视图,如presenting一个 UIAlertView中我需要显示 UIAlertView中在主线程。要做到这一点,我用下面的code。

Example: I'm performing some logic within the block of an NSURLConnection sendAsynchronousRequest:urlRequest method. Because I'm doing stuff to the main view such as presenting a UIAlertView I need to show the UIAlertView on the main thread. To do this I'm using the following code.

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    // code snipped out to keep this question short

    if(![NSThread isMainThread]) 
    {
        dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Some Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alertView show];
        });
    }
}];

在同一个如果(![NSThread isMainThread])语句我还呼吁一些自定义的方法。现在的问题是,我应该用我使用上面还是最好使用 performSelectorOnMainThread 不是 dispatch_async 方法?例如,下面满code:

Within that same if(![NSThread isMainThread]) statement I also call some custom methods. The question is, should I use the dispatch_async method that I'm using above or is it better to use performSelectorOnMainThread instead? For example, full code below:

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    // code snipped out to keep this question short

    if(![NSThread isMainThread]) 
    {
        dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Some Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alertView show];

            // call custom methods in dispatch_async?
            [self hideLoginSpinner];
        });

        // or call them here using performSelectorOnMainThread???
        [self performSelectorOnMainThread:@selector(hideLoginSpinner) withObject:nil waitUntilDone:NO];
    }
}];

仅供参考 - 如果我不上他执行这些操作的主线程我看到几秒钟的延迟时presenting的 UIAlertView中我在收到以下消息调试器 wait_fences:未能收到回复:10004003 。我了解到,这是因为你需要更改主线程的UI ......如果有人想知道为什么我在做我在做什么...

FYI - If I DO NOT perform these actions on he main thread I see a few second delay when presenting the UIAlertView and I receive the following message in the debugger wait_fences: failed to receive reply: 10004003. I've learned that this is because you need to make changes to the UI on the main thread... In case someone is wondering why I'm doing what I'm doing...

推荐答案

作为由Josh卡斯威尔提供的链接所提到的,两者几乎等同。最显着的区别是 performSelectorOnMainThread 将在默认的运行循环模式只执行,如果运行的循环是在跟踪或其它模式下运行将等待。但是,也有用于写入和维持code一些显著差异。

As mentioned in the links provided by Josh Caswell, the two are almost equivalent. The most notable differences is that performSelectorOnMainThread will only execute in the default run loop mode and will wait if the run loop is running in a tracking or other mode. However, there are some significant differences for writing and maintaining the code.


  1. dispatch_async 有很大的优势,编译器做了所有常用的测试。如果你在错误键入 performSelectorOnMainThread 的方法,你不能在运行时,而不是编译时间。

  2. dispatch_async 使得它更容易从主线程使用 __块预选赛返回数据。

  3. dispatch_async 使得它更容易处理的原始参数,因为你不必将它们包装在一个对象。然而,这种带有一个潜在的缺陷。如果一些数据记住块捕获不深的数据复制有一个指针。在另一方面包裹数据中的对象,你将不得不为 performSelectorOnMainThread 做深做副本(除非你设置特殊选项)。没有深刻的副本,你可以遇到间歇性的错误是令人沮丧的调试。因此,这意味着你应该换行像的char * 的NSString 的东西,你叫 dispatch_async

  1. dispatch_async has the big advantage that the compiler does all its usual tests. If you mistype the method in performSelectorOnMainThread you fail at run time, rather than compile time.
  2. dispatch_async makes it much easier to return data from the main thread using the __block qualifier.
  3. dispatch_async makes it much easier to handle primitive arguments since you don't have to wrap them in an object. However, this comes with a potential pitfall. If you have a pointer to some data remember that block capture does not deep copy the data. On the other hand wrapping the data in an object as you would be forced to do for performSelectorOnMainThread does deep copy (unless you set special options). Without a deep copy you can run into intermittent bugs that are frustrating to debug. So this means you should wrap things like char * in NSString before you call dispatch_async.

这篇关于在主线程使用dispatch_async或performSelectorOnMainThread执行UI的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:12