iPhone – Why is NSTIMER when another thread is running?

I am trying to run a long task in the background of the iPhone. I start it with performSelectorInBackground. I also create an NSTimer on the main thread to check if it works. I expect a timer Will run when another thread is running:

- (void)viewDidLoad
{
[super viewDidLoad];
< br /> [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!");
}

lenghtyMethod performs many things, including URL downloading using ASIHTTPRequest, etc.

The output of NSLog is as 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 seems to be blocking the timer. My understanding of this is that performSelectorInBackground should be run in a new thread separate from the main loop.

I don’t understand. When When the thread is running, I did not output from the timer. After the call is completed, the timer starts recording again.

For recording, the thread is mainly doing I/O (loading URL), so the operating system should have enough Time to switch threads. This happens on both the simulator and the actual device.

According to ASIHTTPRequest:

For more complex situations, or where you want to parse the response in the background, create a minimal subclass of ASIHTTPRequest for each type of request, and override requestFinished: and failWithProblem:.

Otherwise, if the default callbacks are used, they will run on the main thread.

I am Trying to run a long task in the background of the iPhone. I started it with performSelectorInBackground. I also created an NSTimer on the main thread to check if it is valid. I expect the timer will run when another thread is running:

< /p>

- (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! ");
}

lenghtyMethod performs many things, including URL downloading using ASIHTTPRequest, etc.

The output of NSLog is as follows:

< /p>

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 seems to be blocking the timer. My understanding of this is that performSelectorInBackground should be in a new thread separate from the main loop Run.

I don’t understand. When the thread is running, I don’t output from the timer. After the call is completed, the timer starts recording again.

For recording, the thread mainly performs I/O (load URL), so the operating system There should be enough time to switch threads. This happens on both the simulator and the actual device.

According to ASIHTTPRequest:

For more complex situations, or where you want to parse the response in the background, create a minimal subclass of ASIHTTPRequest for each type of request, and override requestFinished: and failWithProblem:.

p>

Otherwise, if the default callbacks are used, they will run on the main thread.

Leave a Comment

Your email address will not be published.