• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Asynchronous call of Selector - is this code OK?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Asynchronous call of Selector - is this code OK?
      • From: Manfred Lippert <email@hidden>
  • Prev by Date: Re: reading in text files
  • Next by Date: Re: Possibly OT: Mouse Acceleration
  • Previous by thread: Asynchronous call of Selector - is this code OK?
  • Next by thread: Re: Asynchronous call of Selector - is this code OK?
  • Index(es):
    • Date
    • Thread