开发者

Put run-loop-based NSThread to sleep for an indeterminate amount of time

开发者 https://www.devze.com 2023-02-18 12:31 出处:网络
I have a dedicated networking thread in my iOS app. The thread\'s -main method looks like this: - (void)main

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消