I have a dedicated networking thread in my iOS app. The thread's -main method looks like this:
- (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 (concurrent, run-loop-based NSOperation subclass). Now since I know when there are http operations in need of the runloop thread, I would like to be able to prevent the runloop thread from executing when there is no work to do. I read somewhere (helpful, I know) that NSRunLoop automatically sleeps when it has no work to do. However, I used Instruments' time profiler and saw that the 开发者_如何学编程runloop thread was always active.
How can I arrange my use of NSRunLoop and NSThread to prevent this waste of resources?
精彩评论