1

I have a few functions that I need to call when I press a button. I use [self getamountofdatacenters]; to call these functions. When I press the button, the simulator gets stuck until its done with all the functions, and this takes 5-7 seconds, so I'm trying to use NSThread:

[NSThread detachNewThreadSelector:@selector(GETAMOUNTDATACENTERS) 
                         toTarget:self 
                       withObject:nil];

I'm doing this three times with different functions, but I want to find out what the progress is of an NSThread and to catch it when it's actually done. How can I do this? Thank you.

jscs
  • 63,694
  • 13
  • 151
  • 195
Emre Akman
  • 1,212
  • 3
  • 19
  • 25

2 Answers2

3

Forget about explicit threads and use GCD:

dispatch_async(dispatch_get_global_queue(0, NULL), ^{
    [self getNumberOfDataCenters];
    dispatch_sync(dispatch_get_main_queue(), ^{
        // The work is done, do whatever you need.
        // This code is executed on the main thread,
        // so that you can update the UI.
    });
});
Community
  • 1
  • 1
zoul
  • 102,279
  • 44
  • 260
  • 354
1

One option is to "catch" the end of a thread's execution at the application level: in the code you post to an alternate thread, fire an NSNotification just as it completes. In the main thread you observe this notification and react accordingly.

You can also inspect a thread's state using -isExecuting, -isFinished and -isCancelled.

  • How can i find out when it completes? do i need to call a NSNotification in the end of a function that im calling ?, and how can i observe it? could you show me an example ? i would realy appreciate it! – Emre Akman May 17 '11 at 07:23