我寫了一方法如下:
- (BOOL)shouldDoSomeWork {
BOOL result = // here I need do hard work with data in background thread and return result, so main thread should wait until the data is calculated and then return result;
return result;
}
如何實現?
-(void) startWork
{
[NSThread detachNewThreadSelector:@selector(doSomeWork) toTarget:self withObject:nil];
}
-(void) doSomeWork
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
[pool release];
[self performSelectorOnMainThread:@selector(doneWork) withObject:nil waitUntilDone:NO];
}
-(void) doneWork
{
}