vdwivedi
vdwivedi

Reputation: 79

Difference between perform selector in backgorund and detachNewThread

I want to know what is difference between perform selector in backgorund and detachNewThread

Upvotes: 1

Views: 650

Answers (2)

Wolverine
Wolverine

Reputation: 4329

They Are identical. as you can see in Documentation section Click Here

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.

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

Upvotes: 2

Christian Stieber
Christian Stieber

Reputation: 12496

I would look at it from a semantic point of view. There is no technical reason to use one or the other.

Use NSThread if you actually "think" of having a thread that "does something"; in particular, it will probably be the most appropiate way of creating a thread if your thread runs some form of event- or messaging loop. In such a case, the "thread object" is really just that; in many cases it's not an "application realm" object with actual application data, as these will be handed over to the thread in some way.

Use the NSObject-based methods if your thread is merely meant to run some single operation in the background. You don't really care about this being a "thread", and the object that you run this on is likely to be the "application realm" object with the data; there's no event- or messageloop to feed it commands from other threads.

Thus, I would base the decision on abstract factors, as in "what looks better in the given context". Having an NSThread "feels" like a more detached entity that is willing to offer services to multiple clients, whereas the NSObject method feels like it's closely attached to the data object that it runs with, and doesn't really deal with anything else unless it's vital to the cause.

Upvotes: 0

Related Questions