Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between [NSThread detachNewThreadSelector:] and -performSelectorInBackground

I've been using -performSelectorInBackground in many of my apps, sort of oblivious to -detachNewThreadSelector. Now I am wondering what the differences are between the two. Are they pretty much interchangeable, or are there differences and places where one is superior to the other? Thanks!

like image 537
mahboudz Avatar asked Jan 19 '10 09:01

mahboudz


3 Answers

They're identical. See documentation.

performSelectorInBackground:withObject: The effect of calling this method is the same as if you called the detachNewThreadSelector:toTarget:withObject: method of NSThread with the current object, selector, and parameter object as parameters.

like image 70
Michael Waterfall Avatar answered Nov 18 '22 14:11

Michael Waterfall


As Michael says, they are identical. I think, to use a thread by performSelectorInBackground:withObject: is easier way rather than NSThread.

However, NSThread can control its priority, stacksize, etc. If you'd like to customize the behavior, I recommend NSThread instead of performSelectorInBackground:withObject:.

like image 5
KatokichiSoft Avatar answered Nov 18 '22 15:11

KatokichiSoft


Corey, if you're running on a new thread and you're using [object autorelease], you'll need to create your own autorelease pool. Fairly simple to do:

- (void)uploadDataOnThread
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // do something here...

    [pool release];
}
like image 3
Mr. Propa Avatar answered Nov 18 '22 14:11

Mr. Propa