I want to ask some simple but important questions regarding iPhone development. If we have to perform tasks in the background and when the background tasks are completed we will update the UI, for this we can use NSThreads, NSOperations or (performSelector)performSelectorInBackgroundThread. What is the difference between all these and how they will effect my apps performance.
One more thing, what is the difference between these two statements written bellow:-
[self getData];
[self performSelector:@selector(getData)];
Please explain as i dont know the difference between all these things.
There is actual not a big difference between
AND
The only difference is when you call [self getData] the compiler can determine that you want to send getData message to the object of class
[self class]. And if it can’t find any method prototypes, that were declared earlier there would be a warning.The first and the second lines would be translated to
performSelector:is really cool thing when you want to do something at runtime (for example you determine at runtime what exact message you want to send to the object). Or here is an example from the “real life”: UIButton has methodSo it stores the action somewhere in it’s internals and when there is appropriate control event it calls:
NSOperation is just a nice way to wrap your work with threads. And NSThread is just wrapper to pthreads.
So your app performance doesn’t really depend on the way you’re using threads, however it’s much easier to do that using NSOperation rather than pthreads.