Does detachNewThreadSelector work any different than performSelectorInBackground? In my project I use a lot of this:
[self performSelectorInBackground:@selector(startImageDownloads:) withObject:[NSNumber numberWithInt:dataType]];
but would doing this be any different:
[NSThread detachNewThreadSelector:@selector(startImageDownloads:) toTarget:self withObject:[NSNumber numberWithInt:dataType]];
And also, besides being able to access the thread object with imgDlThread, would alloc/init’ing a thread then starting it work any different then the first 2:
NSThread *imgDlThread = [[NSThread alloc] initWithTarget:self selector:@selector(startImageDownloads:) object:[NSNumber numberWithInt:dataType]];
[imgDlThread start];
Thanks!
Edit:
Just realized there’s several answers on SO already for the difference (or lack of) between performSelectorInBackground and detachNewThreadSelector, so I guess my only question is:
Is allocating and initializing a NSThread then calling [thread start] any different then the first 2?
The only difference between the third method and the first two is memory management. When you allocate a thread, it retains it’s
target, only to be released when the thread is deallocated. ThedetatchNewThreadSelector:andperformSelectorInBackground:method both autorelease the resultingNSThreadthat is created, meaning that, once the thread finishes execution, thetargetwill be released.In the code that you provided (allocate a thread and start it), you are leaking
imgDlThread, meaning thattargetwill never be released, and in turn will be leaked itself. If you autorelease or even regular releaseimgDlThreadafter starting it, it will have the exact same effect asdetachNewThreadSelector:.