Objective-C performSelectorInBackground - 在哪里调用方法运行?

时间:2023-01-15 23:01:12

Simple question: what happens if I do this:

简单的问题:如果我这样做会发生什么:

- (void)viewDidLoad
{
    [self performSelectorInBackground:@selector(myBGMethod) withObject:nil];
}

-(void)myBGMethod
{
    [self myOtherMethod];
}

-(void)myOtherMethod
{
    NSLog(@"This is not Inception");
    //some more code here
}

Will the NSLog() and other code in myOtherMethod be run on the main thread or in the background?

myOtherMethod中的NSLog()和其他代码是在主线程还是在后台运行?

3 个解决方案

#1


1  

It'll be run in the background thread.

它将在后台线程中运行。

You can confirm this by calling NSLog inside all your methods. By default, NSLog prints the thread number along the process ID (pid).

您可以通过在所有方法中调用NSLog来确认这一点。默认情况下,NSLog沿进程ID(pid)打印线程号。

#2


1  

It'll be run in the background. Once you make the call to myBGMethod in another thread, whatever it calls is made on that same thread unless it specifically requests another thread.

它将在后台运行。一旦你在另一个线程中调用myBGMethod,它调用的是在同一个线程上进行的,除非它特意请求另一个线程。

As a side note, depending on which version of iOS you want to support, you might want to learn more about Grand Central Dispatch. It makes multithreading a lot simpler.

作为旁注,根据您要支持的iOS版本,您可能想要了解有关Grand Central Dispatch的更多信息。它使多线程变得更加简单。

#3


1  

If you are ever curious about what thread a particular line of code is executing on, you can put a breakpoint on that line and check the Debug Navigator pane in Xcode:

如果您对某个特定代码行正在执行的线程感到好奇,可以在该行上放置一个断点并检查Xcode中的Debug Navigator窗格:

Objective-C performSelectorInBackground  - 在哪里调用方法运行?

In this case, I put a breakpoint on NSLog(...)

在这种情况下,我在NSLog上放了一个断点(...)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"HI!");
});

and you can see that we're in Thread 2 com.apple.root.default-priority

你可以看到我们在线程2 com.apple.root.default-priority

#1


1  

It'll be run in the background thread.

它将在后台线程中运行。

You can confirm this by calling NSLog inside all your methods. By default, NSLog prints the thread number along the process ID (pid).

您可以通过在所有方法中调用NSLog来确认这一点。默认情况下,NSLog沿进程ID(pid)打印线程号。

#2


1  

It'll be run in the background. Once you make the call to myBGMethod in another thread, whatever it calls is made on that same thread unless it specifically requests another thread.

它将在后台运行。一旦你在另一个线程中调用myBGMethod,它调用的是在同一个线程上进行的,除非它特意请求另一个线程。

As a side note, depending on which version of iOS you want to support, you might want to learn more about Grand Central Dispatch. It makes multithreading a lot simpler.

作为旁注,根据您要支持的iOS版本,您可能想要了解有关Grand Central Dispatch的更多信息。它使多线程变得更加简单。

#3


1  

If you are ever curious about what thread a particular line of code is executing on, you can put a breakpoint on that line and check the Debug Navigator pane in Xcode:

如果您对某个特定代码行正在执行的线程感到好奇,可以在该行上放置一个断点并检查Xcode中的Debug Navigator窗格:

Objective-C performSelectorInBackground  - 在哪里调用方法运行?

In this case, I put a breakpoint on NSLog(...)

在这种情况下,我在NSLog上放了一个断点(...)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"HI!");
});

and you can see that we're in Thread 2 com.apple.root.default-priority

你可以看到我们在线程2 com.apple.root.default-priority