There are many reasons for using NSThread in your application based on the architecture and complexity of the application. The main purpose to use threads in application, is concurrency and asynchronously working for process and send notification message to main thread. By this way, developer doesn’t needs to block main thread while applications are executing different tasks.
NSOperation is used for the same concurrency purpose as same as NSThread. NSOperation is a best way to wrap process with threads while NSThread is just wrapper to pthreads.
Professional iOS developer can use either NSOperation or NSTread to achive asynchronouse behaviour in application. App performance doesn’t really rely on the way of using threads. However, it is much easier to implement and user NSOperation instead of pthreads.
How NSOperation and NSThread Works:
1. iOS developers have to write code for the work/process he want to perform along with for the creation and management of the threads themselves.
2. iOS developers have to be careful about a plan of action for using threads.
3. iOS developer have to manage posiable problems like reuseability of thread, lockings etc. by them self.
4. Thread will consume more memory too.
1. The NSOperation class is an abstract class which encapsulates the code and data associated with a single task.
2. Developer needs to use subclass or one of the system-defined subclasses of NSOperation to perform the task.
3. Add operations into NSOperationQueue to execute them.
4. The NSOperationQueue creates a new thread for each operation and runs them in the order they are added.
5. Operation queues handle all of the thread management, ensuring that operations are executed as quickly and efficiently as possible.
6. An operation queue executes operations either directly by running them on secondary threads or indirectly using GCD (Grand Central Dispatch).
7. It takes care of all of the memory management and greatly simplifies the process.
8. If you don’t want to use an operation queue, you can also execute an operation by calling its start method. It may make your code too complex.
How To Use NSThread And NSOperation:
1. Though Operation queues is the preferred way to perform tasks concurrently, depending on application there may still be times when you need to create custom threads.
2. Threads are still a good way to implement code that must run in real time.
3. Use threads for specific tasks that cannot be implemented in any other way.
4. If you need more predictable behavior from code running in the background, threads may still offer a better alternative.
1. Use NSOperation when you have more complex operations you want to run concurrently.
2. NSOperation allows for subclassing, dependencies, priorities, cancellation and a supports a number of other higher-level features.
3. NSOperation actually uses GCD under the hood so it is as multi-core, multi-thread capable as GCD.
Now you should aware about advantages and disadvantages of NSTread and NSOperation. You can use either of them as per needs of your application.