NSTimer questions

Discussion in 'iPhone/iPad Programming' started by mrl72, Feb 4, 2011.

  1. macrumors regular

    #1
    Hi all, I have a couple of questions regarding the NSTimer.

    1. From my main thread I call two timers, one plays an image slideshow in a 10 second loop, and the other downloads images from a server. When the second timer is running, the slideshow pauses until it completes. Any idea why this would happen?

    2. I want to add the ability to automatically kick off a method if the user quits the apps and returns after a specified time. Can this be achieved via a timer or is there some kind of built in method that I can use?

    Cheers.
     
  2. macrumors 6502

    NickFalk

    #2
    1. Some code-snippets would be nice.

    2. How about using an NSDate as a timestamp, storing it and then comparing it with the time the user reenters the app?
     
  3. macrumors regular

    #3
    Thanks Nick. I'll paste some code shortly.

    In regards to your second answer, how would this work exactly? Is it case of having a timer run repeatedly (say every minute) that simply looks for that date and then the date is set in applicationDidEnterBackground and/or applicationWillTerminate methods?

    Cheers.
     
  4. macrumors 6502

    NickFalk

    #4
    Nope you just make an NSDate with the current time at the time of exit. When the user return you compare the stored exitDate to the time the time at the moment. Something along these lines:

    NSTimeInterval elapsedTime = [reEnterDate timeIntervalSince: exitDate]
     
  5. macrumors regular

    #5
    Excellent, thank you. I did that and it worked great!
     
  6. Moderator

    dejo

    Staff Member

    #6
    Sounds like what you are doing in the second bit is blocking the main run loop. And the UI can only be updated from the main thread. You should find a way to put your image downloading into the background.
     

Share This Page