Difference between performSelectorInBackground and NSOperation Subclass

Posted by AmitSri on Stack Overflow See other posts from Stack Overflow or by AmitSri
Published on 2010-05-29T12:25:38Z Indexed on 2010/05/29 12:32 UTC
Read the original article Hit count: 630

I have created one testing app for running deep counter loop. I run the loop fuction in background thread using performSelectorInBackground and also NSOperation subclass separately.

I am also using performSelectorOnMainThread to notify main thread within backgroundthread method and [NSNotificationCenter defaultCenter] postNotificationName within NSOperation subclass to notify main thread for updating UI.

Initially both the implementation giving me same result and i am able to update UI without having any problem. The only difference i found is the Thread count between two implementations.

The performSelectorInBackground implementation created one thread and got terminated after loop finished and my app thread count again goes to 1.

The NSOperation subclass implementation created two new threads and keep exists in the application and i can see 3 threads after loop got finished in main() function.

So, my question is why two threads created by NSOperation and why it didn't get terminated just like the first background thread implementation?

I am little bit confuse and unable to decide which implementation is best in-terms of performance and memory management.

Thanks

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa