I have a dedicated network stream in my iOS app. The thread-main method is as follows:
- (void)main { @try { while (!self.isCancelled) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [[NSRunLoop currentRunLoop] run]; [pool drain]; } } @catch (NSException * e) { NSLog(@"%@", e); } }
I have an NSOperationQueue populated with HTTP operations (a parallel loop-based subclass of NSOperation). Now, since I know when there are HTTP operations that need a runloop thread, I would like to be able to prevent the runloop thread from executing when there is no work. I read somewhere (useful, I know) that NSRunLoop automatically sleeps when it has no work. However, I used the tool profiler and saw that the runloop thread was always active.
How can I organize the use of NSRunLoop and NSThread to prevent this waste of resources?
source share