nstimer

iPhone - NSTimer not repeating after fire

天大地大妈咪最大 提交于 2019-12-04 01:45:26
I am creating and firing a NSTimer with: ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]; [ncTimer fire]; AND - (void)handleTimer:(NSTimer *)chkTimer { // do stuff } I am retaining my timer with: @property (nonatomic, retain) NSTimer *ncTimer; For some reason the timer is not repeating. It is firing once only and than never again. You can't just assign to the timer that you have put as a property in your header. This should work: self.ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector

How to check if NSTimer has been already invalidated

跟風遠走 提交于 2019-12-04 00:35:59
I have a NSTimer object that I need to invalidate if a user taps on a button or if they exit a view. So I have: [myNSTimer invalidate]; inside my button handler and inside viewWillDisappear . If user taps on a button and then exists a view the app throws an exception because myNSTimer has already been invalidated. What I need to do in the viewWillDisappear method is check if the myNSTimer has been invalidated or not. How do I do that? I've tried: if(myNSTimer != nil) [myNSTimer invalidate]; but that didn't work. Once you invalidate the timer, simply call release on it (assuming you've retained

Better alternative for NSTimer

只愿长相守 提交于 2019-12-03 22:33:33
I'm just wondering, is there a better, more efficient function to use than NSTimer? I have a NSTimer code like this on my ViewDidLoad function: [NSTimer scheduledTimerWithTimeInterval:900.0f target:self selector:@selector(methodToRun) userInfo:nil repeats:YES]; With the function methodToRun: -(void)methodToRun { if(currentTime == presetTime) { //do something } } This works fine but the problem is, this eats up a lot of memory and I am getting memory warnings. So, what's a better, more efficient and less memory consuming way of triggering my methodToRun continuously to check if the currentTime

Stop and restart a timer

早过忘川 提交于 2019-12-03 21:47:26
I want to stop this timer and then restart it from where I stopped it. secondsTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(addSeconds), userInfo: nil, repeats: true) Below, it was suggested I shouldn't increment a timer in my timer handler. Why not? For example, using GCD timer: func countSeconds() { secondsTimer = DispatchSource.makeTimerSource(queue: .main) secondsTimer?.schedule(deadline: .now(), repeating: 1.0) secondsTimer?.setEventHandler { [weak self] in self?.addSeconds() } } @objc func addSeconds() { seconds += 1 } func startGame() { secondsTimer?

Keep an NSThread containing an NSTimer around indefinitely? (iPhone)

六月ゝ 毕业季﹏ 提交于 2019-12-03 21:43:50
I have some web service data in my app that needs to be updated every 3 minutes. I had tried out a few approaches but got a really good piece of advise in here last week, I should not build a new thread every 3 minutes and then subsequently try and dealloc and synchronize all the different parts so that I avoided memory bug. Instead I should have a "worker thread" that was always running, but only did actual work when I asked it too (every 3 minutes). As my small POC works now, I spawn a new thread in the applicationDidFinishLaunching method. I do this like so: [NSThread

iOS Countdown Timer To Specific Date

三世轮回 提交于 2019-12-03 21:40:49
I am trying to get a countdown timer to a specific date. I use: -(void) viewWillAppear:(BOOL)animated { NSString *str =@"12/27/2013"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"MM/dd/yyyy"]; NSDate *date = [formatter dateFromString:str]; NSTimeInterval numberOfSecondsUntilSelectedDate = [date timeIntervalSinceNow]; NSInteger numberOfDays = numberOfSecondsUntilSelectedDate / 86400; startTime = [[NSDate date] retain]; secondsLeft = numberOfDays; NSLog(@"number%f", numberOfSecondsUntilSelectedDate); [self countdownTimer]; } - (void)updateCounter:

Set timer to UIPageViewController

一世执手 提交于 2019-12-03 21:36:31
I'm trying to have the UIPageViewController navigate through the pages for every 5 seconds. How to do this in Swift? Is there any way to explicitly call viewControllerAfterViewController ? var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("update"), userInfo: nil, repeats: true) func update() -> UIViewController { // can u please tell me what to add here } func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { var index = (viewController as

Is it safe to schedule and invalidate NSTimers on a GCD serial queue?

牧云@^-^@ 提交于 2019-12-03 17:50:02
问题 What's the right way to do this? The NSTimer documentation says this: Special Considerations You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly. Since GCD doesn't assure you that a serial queue will always run blocks on the same thread, what's the right way to ensure that you schedule and

Invalidating an NSTimer in dealloc

非 Y 不嫁゛ 提交于 2019-12-03 17:17:02
Following this question , and more specifically, this comment : because retain (aka strong reference) cycles in the common case where the timer's target is also its owner I am wondering why dealloc isn't a good place to invalidate an NSTimer . I remember profiling my app without auto-repeating NSTimer invalidation and then with invalidation in dealloc , and the memory correctly freed. Is dealloc working differently in the latest iOS? Isn't in fact your overridden dealloc called prior to any NSObject deallocation? What is dealloc even used for, then? If not manually deallocating the respective

How to run NSTimer in background and sleep in iOS?

一曲冷凌霜 提交于 2019-12-03 11:17:14
问题 I found a lot of post in stackoverflow about NSTimer run in background. However I didn't find any solution. In my app , I play sound in background and I set timer to stop music when it reached that time. So I need to run my NSTimer background (mean when home button click and sleep iPhone). How can I do that? 回答1: // NSTimer run when app in background <br> [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; loop = [NSTimer scheduledTimerWithTimeInterval:0.25