sleep() question

Discussion in 'iOS Programming' started by JoshAK, Dec 10, 2008.

  1. macrumors newbie

    Joined:
    Aug 12, 2008
    #1
    I'm trying to write a script that will update an image then pause or sleep for 2 seconds and then set a different image.

    Currently I'm using sleep(2). But it sleeps first and then updates the images after. I want to update first then pause then update again. Any help would be nice. Thanks
     
  2. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    Any sleep() or delay inside the main thread blocks all UI activity, including pending display updates. Thus, it's the wrong paradigm for Cocoa. Don't do it.

    Instead, learn to use NSTimers to call your methods, and let these methods do what you want to do at any point in time in the future. You may have to save the state of what you want to do next or later in static, global, or object variables, but that's a trivial problem for the student.

    The only time this may be difficult is inside deeply recursive routines. You may want to run those in a background (non UI) thread instead.

    .
     
  3. thread starter macrumors newbie

    Joined:
    Aug 12, 2008
    #3
    Thanks!

    Thanks for the reply. Can you give me an example of how I could us an NSTimer to delay or pause for 2 seconds? I'm new to xcode/cocoa and I'm not sure how to accomplish this. Thanks for the help.
     
  4. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
  5. thread starter macrumors newbie

    Joined:
    Aug 12, 2008

Share This Page