I am observing a strange behavior. I am executing this method:
- (void) aLongRunningMethod: (NSString*) input {
for (int i = 0; i < 100; ++i) {
//Sleep for 1 second
[NSThread sleepForTimeInterval: 1];
NSLog(@"Progress: %d", i);
}
}
In the iOS simulator, I hit the home button. I expect the thread to be suspended right away. But, that does not happen. The method keeps going. I am pretty sure that that is not the designed behavior. Is this a defect in the simulator or am I mi开发者_运维百科sunderstood about application's state transition.
Anything running on the current/main runloop with have a chance to finish. But the operating system reserved the rights to kill anything taking too long.
If you execute that method on a background thread it will suspend right away when you press the Home button.
Try it:
[self performSelectorInBackground:@selector(aLongRunningMethod:) withObject:@"Running in background"];
精彩评论