I am trying to run a small task in the background on an iPhone. I start with performSelectorInBackground. I also create NSTimerin the main thread to check if things work. I expected the timer to work while another thread did this:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(onTimerEvent:)
userInfo:nil repeats:YES];
[self performSelectorInBackground:@selector(lengthyMethod) withObject:nil];
NSLog(@"Here we go!");
}
- (void)onTimerEvent:(NSTimer *)timer
{
NSLog(@"timer!");
}
lenghtyMethoddoes a lot of things, including loading URLs using ASIHTTPRequest , etc.
The way out is NSLogas follows:
2011-03-11 15:17:07.470 MyApp[6613:207] Here we go!
2011-03-11 15:17:07.570 MyApp[6613:207] timer!
2011-03-11 15:17:07.670 MyApp[6613:207] timer!
// ... several seconds of output from lenghtyMethod omitted ...
2011-03-11 15:17:11.075 MyApp[6613:207] timer!
2011-03-11 15:17:11.170 MyApp[6613:207] timer!
// ... etc ... timer runs as expected when the call is completed ...
The problem is that the background thread is blocking the timer . My understanding of this was that performSelectorInBackground should run in a new thread separately from the main loop.
. , . , .
- ( URL-), . , .