3

I'm confused about the concept of "threads" in iPhone development:

  • Why are threads necessary / useful?
  • How can threads be used in Objective-C?
Shog9
  • 156,901
  • 35
  • 231
  • 235
user428149
  • 99
  • 1
  • 1
  • 5
  • possible duplicate of [Where can I find a good tutorial on iPhone/Objective c multithreading?](http://stackoverflow.com/questions/1004845/where-can-i-find-a-good-tutorial-on-iphone-objective-c-multithreading) – Brad Larson Sep 15 '10 at 13:35
  • 3
    These are very general questions... can you give a specific indication of what you're trying to achieve? Or are we just doing your homework for you? – Ashley Mills Sep 13 '11 at 10:44
  • @BradLarson You as a moderator are referring to a 'not constructive' question?!!? :| – mfaani Mar 22 '16 at 16:06

4 Answers4

6

You need multi-threading in objective c because sometimes you need functions/code to run "in the background" (read: on another thread). For instance (but not explicitly) you might need to download large amounts of data off the internet (a picture, or a video).

In this case running the download on the 'main' thread will cause the iphone to freeze before the download is complete. So you use multi-threading to download the data AND let the iphone work all at the same time.

There are lots of ways to do multithreading in objective-c. To be honest you need to look it up yourself, we're not here to just spoonfeed you.

Things to look up are: NSURLConnection and the method [self performSelector:onThread:...]

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
0

More simple...If you want to run some methods(processes) parallely you can use threads...One thread is doing one stuff while another doing other stuff... So u can use threads if you need something to be done when another thing is doing... Example: Thread 1: sending request to server Thread 2: preparing information(image,text etc) to be sent. So in general this is the purpose of threads

Garnik
  • 423
  • 1
  • 6
  • 20
0

Recently, Apple suggests that programmers should move away from thread and use an alternative solution with more advantages, better performances and much more easier to implement; it's Concurrency Programming:
http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091

Envil
  • 2,687
  • 1
  • 30
  • 42
  • 1
    These are not competing technologies. Threads are part of "Concurrency Programming" and need to be fully understood to successfully build multithreaded applications. Apple's documentation could be a bit misleading in this context when they proclaim "Migrating away from threads" or so. – Nikolai Ruhe Jun 10 '13 at 19:14
0

The recommended way to implement concurrency is using queues.

For those who just want to execute a method / block in a separate thread - use this code:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
   [self longMehtod];
});

for further information read the Concurrency Programming Guide from Apple

Thorsten Niehues
  • 13,712
  • 22
  • 78
  • 113