Bit of a noob here.
I am developing an app that plays some looped sounds. I would like to give the user the ability to shut down the app after a certain amount of time using a timer. The idea is the user presses a button and the app will shut down once the timer runs out.
At the moment if the button is pressed, the app crashes.
Here's what I got so far:
- (IBAction)timer:(id)sender{
timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
}
-(void) targetMethod: (NSTimer*) theTimer {
NSLog(#"timer?");
exit(0);
}
You need to properly define your timer reference:
NSTimer *timer = [NSTimer scheduledTimerWithInterval: 10.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
Related
I'm using XCode 5. I'm making a game using Cocos2D. And want some functions to do repeatedly in sequence and one after another. Due to many actions performed at a time, so wants a delay too (by which no action is interfered by the next one and not cancelling the effect of previous one or rescheduling and perform the function immediately).
My interface freezes and in Debug Navigator, CPU consumption reaches between 99-100% and memory gradually increasing and remain increasing. I put all exceptions breakpoint, too. But no exception arises.
I'm using this code
-(void)threadMainRunLoop
{
BOOL exitNow = NO;
int loopCount = 1;
[self function1];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// Add the exitNow BOOL to the thread dictionary.
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
[threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:#"ThreadShouldExitNow"];
// Install an input source.
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(function2) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function3) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function4) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:#selector(function5) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:#selector(function6) userInfo:nil repeats:NO];
while (loopCount <= 4 && !exitNow)
{
// Do one chunk of a larger body of work here.
// Change the value of the moreWorkToDo Boolean when done.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(function2) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function3) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(function4) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:#selector(function5) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:#selector(function6) userInfo:nil repeats:NO];
// Run the run loop but timeout immediately if the input source isn't waiting to fire.
[runLoop runUntilDate:[NSDate dateWithTimeInterval:5.0 sinceDate:[NSDate date]] ];
// Check to see if an input source handler changed the exitNow value.
exitNow = [[threadDict valueForKey:#"ThreadShouldExitNow"] boolValue];
++loopCount;
}
}
And calling this function in ccTouchesEnded:withEvent:.
I followed this code from Thread Management of "Listing 2-3 Checking for an exit condition during a long job".
Is my NSRunLoop make my game/interface to freeze???
Remember device in not hanged. And function1-6 are updating UI. And also when the game will freeze is not expected, may be after some time or after a long time or even may not freeze.
Thanks in advance.
cocos2d is not thread-safe, so if any of these scheduled functions perform cocos2d actions or modify nodes on a background thread there will be problems. Instead use cocos2d's builtin scheduleSelector:interval: methods.
A specific issue here is the while loop. You call runUntilDate which blocks the main thread for 5 seconds, and you do that up to 4 times. Which means cocos2d and ui get to update and draw their state once every 20 seconds. No wonder the app freezes.
If you want to do some multithreading, use performSelectorInBackground or grand central dispatch. Run loops and threads are too low level if you don't control the app's entire run loop and all threads.
This is what I want to do:
I want a timer, to fire a method and then, in the end of this method, be toggled off, and turn on an other timer on another method, and then entering a loop.
So what are the codes used to toggle between on and off the timer on a method?
In Delphi I use:
timer.enable:=True; // timer.enable:=False;
Are there a similar way to do it on objective-c?
I'm using Xcode 4.4
Thanks!
To turn the timer off, call invalidate on your timer like so:
[yourTimer invalidate]
And then to start a new one:
NSTimer *newTimer;
newTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 //Every how many seconds
target:self
selector:#selector(methodToCall)
userInfo:nil
repeats:YES];
Assuming your NSTimer is called "timer", you can use...
[timer invalidate]
to stop the timer. To make a timer pass a message to it's target method instantly, use
[timer fire]
To start a timer, you use one of the constructor methods listed in the documentation (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html) such as
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(doThisWhenTimerFires:) userInfo:nil repeats:NO]
- (void)doThisWhenTimerFires:(NSTimer *)timer
{
//code here
}
I have created a timer,
self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self selector:#selector(meInterrupted:) userInfo:nil repeats:NO];
and some point in time in my code, i feel to disable it before timeout happens, so that the meInterrupted not to be called?. How to achieve this ?
Just call [self.scanTimer invalidate]; This will keep the timer from ever firing if it is called before the timer fires.
Iam displaying time in my application using NSTimer Class starting with 00:00.00 (mm:ss.SS) when my application launched. I want to terminate my application when time reaches to 75:00.00 (mm:ss.SS) in iPhone.
The below code snippet will do the work for you. But its not recommended to use the exit() method to quit the app as it will appear that the app has been crashed to the end user. I recommend you to show an alert after 75 mins and ask the user to quit the app.
[NSTimer scheduledTimerWithTimeInterval:270000
target:self
selector:#selector(quitTheApp)
userInfo:nil
repeats:NO];
- (void)quitTheApp
{
exit(0);
}
Once you start the timer,immediately next line call the below method:
[self performSelector:#selector(stopTimer) withObject:nil afterDelay:75.0];
And stopTimer method should be like this:
-(void) stopTimer
{
[timer invalidate];
timer = nil;
}
I've got a program in which I want to code a timer which checks wether the user is idle or not. For that I wrote following code:
if (!idleTimer) {
NSLog(#"make");
idleTimer=[[NSTimer alloc]initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:10.0]interval:10 target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:NO];
NSLog(#"madetimer with: %f, %#", idleTimer.timeInterval, idleTimer.fireDate);
}else {
NSLog(#"no reset timer: %f", idleTimer.timeInterval);
if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < 9) {
NSLog(#"reset");
[idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
}
But somehow my logs show that the interval is always 0.0000 . Which means that something is wrong here. Can anybody help me?
ive never seen a timer look so complicated. try this:
write a method that checks if the user is idle (lets say idleChecker)
then make the timer repeatable and calls that method idleChecker
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
remember to declare the idleChecker method in the .h file
take note if u want to stop the timer event then you need to maintain a reference to it
NSTimer aTimer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
then to stop it
[aTimer invalidate];
as for checking if its running i would just stick a nslog message in there stating something like "check for idle user"