PDA

View Full Version : NSTimer questions




mrl72
Feb 4, 2011, 06:51 AM
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.



NickFalk
Feb 4, 2011, 06:58 AM
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.

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?

mrl72
Feb 4, 2011, 08:05 AM
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.

NickFalk
Feb 4, 2011, 08:24 AM
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.

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]

mrl72
Feb 4, 2011, 08:56 AM
Excellent, thank you. I did that and it worked great!

dejo
Feb 4, 2011, 10:04 AM
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
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.