Re: Asynchronous call of Selector - is this code OK?
Re: Asynchronous call of Selector - is this code OK?
- Subject: Re: Asynchronous call of Selector - is this code OK?
- From: email@hidden
- Date: Fri, 1 Feb 2002 13:16:47 -0800
is the following code OK, or may it have some strange effects (e.g.
stack
overflow) if "someCondition" will not be TRUE for a longer time?
- (void) delayedMethod() {
// will be "asynchronously" called when "someCondition" becomes TRUE
}
- (void) callDelayedMethodAtSomeCondition {
if (someCondition) {
[self delayedMethod];
} else {
[self
performSelector:@selector(callDelayedMethodAtSomeCondition)
withObject:nil
afterDelay:0.01];
}
}
Oy. I think it will work fine, but please don't do it. :->
The problem is that it is massively inefficient, and is based on
polling. There are lots of other ways to achieve this. Off the top of
my head, you could post your perform selector call when someCondition
becomes true (set a flag to say you've already posted the perform,
cancel previous posts if necessary, etc.), or you could use a
multithreaded design with NSConditionLock.
Spinning the run loop the way you do here wastes CPU time, and keeps
your app and its pages warm even when its sitting in the background
doing nothing at all. That said, I suppose there may be cases when this
is necessary. :->
Ben Haller
Stick Software