tags:

views:

71

answers:

2
-(void)test
{
  int i;
  for (i=0;i < 1000000;i++)
  {
    //do lengthly operation
  }
}

How to prevent its GUI from freezing ?

Thanks in advance.

A: 

I would do the lengthy operation in a separate thread, using NSThread

Prashant
+4  A: 

Bottom line; don't block the main thread and, thus, don't block the main event loop.

Now, you could spawn a thread. But that isn't actually the correct way write concurrent programs on Mac OS X.

Instead, use NSOperation and NSOperationQueue. It is specifically designed to support your concurrent programming needs, it scales well, and NSOperationQueue is tightly integrated into the system such that it will control concurrency based on system resources available (# of cores, CPU load from other applications, etc) more efficiently than any direct use of threads.

See also the Threaded Programming Guide.

bbum