nstimer

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

我怕爱的太早我们不能终老 提交于 2019-12-03 06:33:06
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 invalidate an NSTimer on the same thread? EDIT: Following the advise of the answer below, I created

NSTimers running in background?

倖福魔咒の 提交于 2019-12-03 05:37:14
I was under the impression that NSTimer did not work at all after an application calls applicationWillResignActive . I seems however that existing NSTimers (i.e. ones created before the application resigned active) will continue to run and its only new NSTimers that can't be scheduled in this state, can anyone confirm this? I am also assuming that its good (and Apple seems to say this too) that when your application calls applicationWillResignActive you should disable any NSTimers and start them again when applicationDidBecomeActive is called, does that make sense? When an application is

Passing Data through NSTimer UserInfo

六眼飞鱼酱① 提交于 2019-12-03 03:39:58
问题 I am trying to pass data through userInfo for an NSTimer call. What is the best way to do this? I am trying to use an NSDictionary, this is simple enough when I have Objective-C objects, but what about other data? I want to do something like this, which doesn't work as is: - (void)play:(SystemSoundID)sound target:(id)target callbackSelector:(SEL)selector { NSLog(@"pause ipod"); [iPodController pause]; theSound = sound; NSMutableDictionary *cb = [[NSMutableDictionary alloc] init]; [cb

NSTimer not firing the selector

混江龙づ霸主 提交于 2019-12-03 03:38:18
问题 In ios5.0 with ARC, in my rootviewcontroller I call a method in a security manager object that is held by the app delegate. In that method I setup the timer as below NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; However, this never fires the selector ie. updateModel: never gets called. What may be wrong? Is there another more

Running NSTimer Within an NSThread?

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to run a timer in the background of my application, i am using the timer heavily in my application and i'd rather running it in the background, However i am getting memory leaks when trying to release the NSAoutreleasePool. My Timer class is singleton, so if i start new timer the old timer get dealloc it . + (void)timerThread{ timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil]; //Create a new thread [timerThread start]; //start the thread } //the thread starts by sending this

Stop and Reset NSTimer

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a simple timer using that is activated with a button is pushed. It runs from 60 to 0 no problem but what I want is to stop and reset the timer on push button. I have managed to stop it when the button is pressed using the code below but for some reason cannot get it to reset and stop at 60. This should be simple but it isn't working. Any suggestions? Timer is set using simple Action - (IBAction)timerStart:(id)sender { if(!secondCounter == 0){ [countdownTimer invalidate]; } else { [self setTimer]; } } CODE FOR TIMER - (void)timerRun {

NSTimer Keep counting after app is closed in iPhone

匿名 (未验证) 提交于 2019-12-03 02:28:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a timer that have to count up to 8 hours (28800 second) after that it should be released im wondering how to keep the timer running at the background and/or when application is closed? this is the NSTimer : stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; and this is my condition : counter++; if (counter >= 28800) { [stopWatchTimer invalidate]; counter =0; timeLabel.text = @"Time Out"; } 回答1: You can't - once your app is closed then it's not running

How can I programmatically pause an NSTimer?

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using an NSTimer to do some rendering in an OpenGL based iPhone app. I have a modal dialog box that pops up and requests user input. While the user is providing input I'd like to "pause" i.e. something like this: [myNSTimer pause]; I'm using this syntax because I've been doing things like: [myNSTimer invalidate]; when I want it to stop. How can I programmatically pause the NSTimer? 回答1: From here: http://discussions.apple.com/thread.jspa?threadID=1811475&tstart=75 "You can store the amount of time that has passed since the timer started.

Trivial “+[NSTimer scheduledTimerWithTimeInterval:repeats:block:]: unrecognized selector” error

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am banging my head against an odd error after a move to 10.12/Sierra and Xcode 8.1: +[NSTimer scheduledTimerWithTimeInterval:repeats:block:]: unrecognized selector sent to class 0x7fff78f1fa88 The most minimal code (default settings of create a new project) to reproduce this is: // AppDelegate.m // #import "AppDelegate.h" @interface AppDelegate () @property (weak) IBOutlet NSWindow *window; @property (strong, nonatomic) NSTimer * timer; @end @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

IOS: stop a NSTimer [duplicate]

让人想犯罪 __ 提交于 2019-12-03 01:09:23
Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES]; - (void) targetMethod:(NSTimer) timer{ NSLog(@"Hello World");} in targetMethod I can stop timer with [timer invalidate], but out of this method, How can I stop targetMethod? You can keep your NSTimer in a variable and stop the timer using the invalidate method. Like the following: NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];