Call Method Automatically

Discussion in 'iOS Programming' started by xcodeNewbie, Jul 21, 2011.

  1. xcodeNewbie macrumors member

    Jul 1, 2011
    I'm wondering if there is a way to call a method automatically in xcode. I need a method that is called continuously no matter what, but still leave room for other methods to run. I can't just use a while (1) statement to repeat this method over and over because my other methods (namely my GestureRecognizer methods) won't get called. So I'm wondering if there is a way to make a method that is called every cycle of code.
  2. chown33 macrumors 604

    Aug 9, 2009
    Please explain what needs to run continuously. Be specific.

    There is no way to run something continuously. Periodically, yes. Continuously, no.

    Periodic execution is what a repeating NSTimer is for. If you try to make it more continuous with a very short duration, you'll just bog down the performance of everything else.

    You could also start a background NSThread, but it can't run continously either. If you try, it will also bog down performance, and you'll find that it doesn't run continuously anyway, because other threads will be scheduled which temporarily make the background thread inactive.

    A repeating NSTimer will be much easier to get working than a background NSThread, so I recommend starting with the class reference doc for NSTimer.
  3. xcodeNewbie thread starter macrumors member

    Jul 1, 2011
    Periodic Function

    I'm making a particle simulator, the user can create particles by touching the screen. I have a method that moves the particles according to each of their properties, etc. However, I need the particles to stay in motion even when the user isn't doing anything. I need the method that moves the particles continually called.
  4. chown33 macrumors 604

    Aug 9, 2009
    That's called animation. It's not continuous. It's discrete.

    Animation proceeds frame by frame. Smooth animation has a constant frame rate. A varying frame rate will look jumpy.

    A periodic NSTimer is capable of providing the animation timebase.

    The max frame rate you can achieve will depend on the worst-case time consumed by the particle movement calculations, plus the particle rendering time.
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    xcodeNewbie, based on this and a number of other threads of yours recently, perhaps you should consider looking into using a 2D graphics engine (such as Cocos2D) that already supports such things as particle systems, collision detection, etc.

Share This Page