Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How , Where and When to use NSThread in an Application...iPhone

Tags:

iphone

Every one, As i am new to iPhone Application , now i want know how to use NSThread in my App, what is the use of NSThread.

Please and send me some example programs on that.

like image 629
nikey Avatar asked Dec 06 '22 20:12

nikey


1 Answers

Use NSThread when you want a unit of computational work done without necessarily either waiting for other units to finish, or holding up other computational work.

You can put almost any work into a thread, if it is sensible to do so.

A good example is a network request, where you set up a thread to download data from, say, a web server. Your thread will fire a "handler" function when it has completed its work. The handler works with the downloaded data; for example, parsing XML data from a web service.

You would use a thread in this example, because you don't want the entire application to lock up while your app downloads data over the network and processes it. An NSThread instance puts this unit of work into its own little "space" that allows the larger app to continue to interact with the user.

An example of where you do not want to use threads on the iOS platform is with UI updates (e.g., changing the state of any of the UIControl widgets). All UI updates happen on the main thread. If you use your own threads with UI widgets, the behavior is unpredictable and, more often than not, will simply not work.

like image 176
Alex Reynolds Avatar answered Jan 18 '23 17:01

Alex Reynolds