NSTimer + Modal Problem - objective-c

I'm trying to run a timer while running an NSWindow as modal, but unfortunately it doesn't work at all. The log is called, the window appears and turns modal, but the timer is never called - why? Am I missing something?
NSLog(#"Checking...");
[[NSApplication sharedApplication] runModalForWindow:_Window];
_checkTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(check:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_checkTimer
forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer: _checkTimer
forMode:NSModalPanelRunLoopMode];

If you need for some reasons to add timer after starting modal session - so just add timer to run loop mode: NSModalPanelRunLoopMode
[[NSRunLoop currentRunLoop] addTimer:theTimer
forMode:NSModalPanelRunLoopMode];

Try doing your timer/runloop stuff before starting the modal session.

Related

How to release NSTimer in ASCellNode

I create a NSTimer in ASCellNode, when ASTableView's UIViewController pop, but the timer not be released.
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(autoScrollBanner) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
how can i stop this timer?
thx in advance!
Try overwrite next methods didExitVisibleState, didExitDisplayState,didExitHierarchy in ASCellNode inherited object and insert cancel fired timer.

NSEventTrackingRunLoopMode - this runs always?

I've added a timer with the runloopmode NSEventTrackingRunLoopMode like so:
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.50
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
[runLoop addTimer:timer forMode:NSEventTrackingRunLoopMode];
I thought this would only execute whilst for example a menu is open (modal), but it triggers the entire time, even when doing nothing. Is this the normal behavior?
Yes, this is normal behaviour. You add a timer to the runloopMode which is used for tracking events. This runloop runs all the time and is used to determine if there are events that needs to be processed and passed to your event handlers.

NSTimer not firing

I have an NSTimer that I init with this code:
testTimer = [[NSTimer alloc] initWithFireDate:[new objectAtIndex:0] interval:0.0 target:self selector:#selector(works:) userInfo:nil repeats:NO];
[new objectAtIndex:0] is an NSDate in the past.
When I start up the app, the timer is getting created, with a fireDate of immediately (since the date is in the past), however it never calls my works method. (-(void)works:(id)sender
)
Anybody know why this is happening?
You will have to add it to the current run loop if you use initWith.. method to create the timer object.
NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];
[theRunLoop addTimer:testTimer forMode:NSDefaultRunLoopMode];
Or if you would like it set up for you, use the scheduled... methods to create your timer.
I just recently had an issue with NSTimer. In my case I didn't realize that the method scheduledTimerWithTimeInterval is not multi thread safe. Once I moved the timer to the main thread it started working.
I think I had the same problem as Dobler, but my solution was different.
The problem was that the timer was being created and scheduled in a GCD thread in a block within a
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{})
call (actually nested deep, so it wasn't obvious that this was the case).
Using NSTimer's scheduledTimerWithTimeInterval:... placed the timer into an invalid run loop.
The fix was to change to
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(...) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

Timer invalidate

What is the meaning of this statement given?
NSTimer *timer ,[timer invalidate]
It's an objective C timer statement that cancels a running timer.
Normally it would be expressed as:
NSTimer* myTimer = [NSTimer timerWithTimeInterval:1.0 target:self
selector:#selector(calculateTLE) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode: NSDefaultRunLoopMode];
..
[myTimer invalidate];
It looks as though you're creating an instance of NSTimer without defining it, and then in the same line for whatever reason stopping that same timer. Waste of memory allocation resources, unless you're planning to use the timer at a later time, in which case you should trigger the timer then.

Custom RunLoop Mode

What am I doing wrong here? What am I missing?
- (void)scheduleTimer
{
NSTimer *timer = [NSTimer timerWithTimeInterval:0.15
target:self
selector:#selector(wtf:)
userInfo:nil
repeats:NO];
// This works fine
// [[NSRunLoop currentRunLoop] addTimer:timer
// forMode:NSDefaultRunLoopMode];
// This doesn't work at all - how come?
[[NSRunLoop currentRunLoop] addTimer:timer
forMode:#"MyCustomRunLoopMode"];
}
- (void)wtf:(NSTimer *)aTimer
{
NSLog(#"wtf");
}
The documentation for NSRunLoop seems to indicate one can create custom runloop modes. Am I missing something?
(This is on the main thread of a standard GUI application in Mac OS X)
Update: Notice that I mentioned this was on the main thread of a standard application. Therefore, I'm not running the runloop myself. It's all being handled by NSApplication.
Are you running the runloop for that mode? Just adding a timer won't do anything if the runloop never runs in that mode.
Could it be that the currentRunLoop only runs in common modes?
You should also try:
- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate
and check things out with:
- (NSString *)currentMode
--Tom