> ## Content Index
> Fetch the complete content index at: https://williamboles.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Adding Multi-Threaded Support Simply
- URL: https://williamboles.com/nsinvocationoperation-a-simple-threading/
- Published: 2011-01-21T17:03:00.000Z
- Updated: 2026-09-26T16:34:36.000Z
- Description: Moving work onto a background thread can be hard. Here, we look at how to keep it simple.
- Author: William Boles
- Tags: Concurrency

In iOS, there are two ways to support multi-threading in your app: subclass [NSOperation](https://developer.apple.com/documentation/foundation/nsoperation?language=objc&ref=williamboles.com) or use the more specialised [NSInvocationOperation](https://developer.apple.com/documentation/foundation/nsinvocationoperation?language=objc&ref=williamboles.com). `NSInvocationOperation` (which is a concrete subclass of `NSOperation`) is intended to perform work using the [target-action](https://developer.apple.com/library/archive/documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html?ref=williamboles.com) pattern. So, whereas with a standard `NSOperation` subclass we would create a whole new class, with `NSInvocationOperation` we pass in a `selector` and when that `NSInvocationOperation` instance is executed, it will call that method on a background-thread. For simple tasks, `NSInvocationOperation` offers a lightweight approach to push work onto a background-thread.

![A photo of colourful thread](https://storage.ghost.io/c/b9/b4/b9b42214-5933-4584-8e56-9ee8ca21d23b/content/images/size/w1000/2019/11/colurful-yarn-min-1.jpg)

## Adding multi-threading support

Let's create an `NSInvocationOperation` instance and have it call a method (`foo`) on `self`:

```
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                        selector:@selector(foo)
                                                                          object:nil];
```

`foo` is a simple method that prints out the description of the main-thread and the description of the thread `foo` is being executed on:

```
- (void)foo {
    NSLog(@"Main thread is: %@", [NSThread mainThread]);
    NSLog(@"`foo`, is executing on thread: %@", [NSThread currentThread]);
}
```

Now that we have our operation, we just need to add it to a queue:

```
NSOperationQueue *queue = [NSOperationQueue new];
[queue addOperation:operation];
[operation release];
```

After combining the above snippets, if you run the resulting solution, you should see that `foo` is being called on a background-thread 🕺.

> Please note that you should avoid accessing or updating the UI from a background-thread. Instead, use: `performSelectorOnMainThread:withObject:waitUntilDone:` to push work back onto the main-thread.