Junior — Middle
Explain what an NSThread object is and how it is used to manage threads in iOS or macOS applications.
sobes.tech AI
Answer from AI
NSThread is a class in iOS and macOS that represents a separate thread of execution. It allows creating and managing threads at a low level.
With NSThread, you can:
- Create a new thread in which specific code will be executed.
- Start, pause, and terminate threads.
- Pass data between threads.
Example of creating and starting a thread using NSThread:
NSThread *thread = [[NSThread alloc] initWithBlock:^{
// Code to be executed in the new thread
NSLog(@"Thread started");
}];
[thread start];
However, in modern applications, higher-level APIs such as GCD (dispatch_async) or OperationQueue are more commonly used, as they are simpler and safer to use.