Jack Dawson Jack Dawson - 11 months ago 83
iOS Question

How to create an asynchronous NSoperation iOS?

I have studied Apple documentation about asynchronous operation and I am unable to get it properly.

I am sharing my understanding and efforts. Please take a look and suggest me for understanding asynchronous NSOperation properly.

Asynchronous is a readonly property and we can't change it.
I have created a class that inherits NSOperation. I have override start and main method also.

I am not using NSOperationQueue. When I start operation using [operation start]; method on main thread. Then in start and main method implementation, I get isAsynchronous '0'.

When I start operation on secondary thread using

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
operation = [[CustomOperation alloc] init];
[operation start];

I still getting isAsynchronous '0' in start and main method implementation.

I am not getting why isAsynchronous always returns '0' in any thread. Please let me know the reason behind this.

I saw some questions were asked based on this problem but I couldn't understand this functionality.

It would be helpful for me if you give me any example so that I can understand it properly.

Please let me know if I am not clear enough and I should describe it more.

Answer Source

You are mixing up Grand Central Dispatch and NSOperation.

By default, an NSOperation is synchronous - when you start it, it runs on the current thread until it finishes. You are starting a synchronous NSOperation on a background thread, so it will run on that background thread until it finishes. Which is no problem at all. It runs in the background. It doesn't call itself "asynchronous" because it doesn't care about threads.

Most of the time you are much better off just using Grand Central Dispatch and not using NSOperation at all. The point where you use NSOperation is when you need it to be cancellable.