26

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!

4 Answers 4

34

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.

2
  • That makes sense, since performSelectorInBackground:withObject: is listed under NSObject (NSThreadPerformAdditions) category... so i bet it literally is just a call to detachNewThreadSelector:toTarget:withObject :P
    – Sahil
    Aug 31, 2010 at 0:48
  • Please disregard my last comment, I was talking through my behind.
    – Emmanuel
    Jan 31, 2012 at 14:17
5

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:.

3

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];
}
0
2

Interesting you guys can't point out any differences between them-

I'll tell you this much. We have an iphone app that was locking up while we displayed the "internet activity" disclosure icon in the top bar and a window that said "Sending..." while we sent an audio file up to a server and performSelectorInBackground: was NOT working. When we swapped out performSelectorInBackground with detachNewThreadSelector, it worked fine but now it complains that no autoreleased objects running in that thread are subject to being released by the pool.. maybe this is because the pool is in the main thread?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.