is there timer in ios - iphone

I want to create a timer for example to count 2 sec and after each second an nslog type for example 1 sec passed
any suggestion to do that

yes there is NSTimer, use it as -
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector() userInfo:nil repeats:NO];

What you're after is NSTimer.
Which, I can't not point out, even a cursory search of the framework docs would have turned up. Laziness IS one of the three Virtues of the Programmer, but c'mon.

You can call your NSTimer like this
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(changeValue) userInfo:nil repeats:YES];
changeValue function can be like
-(void)changeValue{
NSLog("calling function after every two seconds");
}

You might want to use NSTimers timerWithTimeInterval:target:selector:userInfo:repeats:
method. Point that towards some object that implements a selector which prints your log entries.

Yes, ios having timer which is used for periodically call method or button touchup event. we can use it like following:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(Your_target_Method)
userInfo:nil
repeats:NO];
NSInteger timer=0;
now call method every 1 sec
-(void)Your_target_Method{
timer=timer+1;
NSLog(#"Timer:%ld",timer);
}
Furthermore, visit https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Timers.html

Related

app freezes, not device hang. In game using cocos2d

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.

How to toggle on/off timer programmatically

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
}

How can I stop my NSTimer thread from further executing the selector

Currently I am creating a NSTimer that calls my method every one second.
I do this like so:
[NSTimer scheduledTimerWithInterval:1 target:self selector:#selector(clocktick) userInfo:nil repeats:YES];
How can I stop this from calling clocktick?
I have tried assigning it like so: NSTimer *myTimer = [NSTimer ... and then using myTimer = nil; but that did nothing.
You need [myTimer invalidate] that will kill the timer for you.
You have to hold a reference to it, and then send the invalidate message to it:
[myTimer invalidate];
I hope it helps!
Have you tried using 'invalidate' ([timerInstance invalidate]; for example)?
Or is the problem that you don't have a reference to the timer instance anymore?

NSTimer not running

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"

I want to call a method after particular time until my app opened in iphone?

I want to call a method after each 2 minutes,
how can I apply such logic?
Use an NSTimer object. It's been talked about quite a lot here, so I won't repeat all that - just search for "NSTimer" in the search box at the top right. :-)
You can use a timer..
[NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:#selector(yourFunction:) userInfo:nil repeats:YES];
.........
..........
-(void)yourFunction:(NSTimer*)timer{
//do your action here
}
I use a NSTimer to trigger a method. I use the below code in my initial viewDidLoad section:
myTimer = [NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:#selector(theMethod) userInfo:nil repeats:YES];
Hope that helps.