I have start a timer to call a method after 10 seconds. the timer start from didFinishLaunchingWithOptions. it works fine but I want to stop that timer from another UISubclass at the time of logout. How I can do this my code is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSThread sleepForTimeInterval:2.0];
[self.window addSubview:viewController.view];
[self setupTimer];
[self.window makeKeyAndVisible];
return YES;
}
-(void)setupTimer;
{
timer = [NSTimer timerWithTimeInterval:10
target:self
selector:#selector(triggerTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)triggerTimer:(NSTimer*)timer;
{
NSLog(#"===============this method is call after every 10 second===========================");
Getlocation *object=[[Getlocation alloc] init];
[object updatelocation];
}
-(void)stopTimer:(NSTimer*)timer;
{
[timer invalidate];
timer=nil;
}
if([timer isValid])
[timer invalidate];
timer = nil;
Take an object of NSTimer.
Write this line where do you want to call method
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(NEWTIMER) userInfo:nil repeats:YES];
Write this code there do you want stop timer.
[timer invalidate];
timer = nil;
[timer release];
if you want stop timer when your app came background .you should write this code into this method.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[timer invalidate];
timer = nil;
[timer release];
}
Write code in logout ButtonAction method.
-(IBAction)LogOutButtonPressed:(id)sender {
[timer invalidate]; timer = nil; [timer release];
}
Related
Hi i want to start a timer with another thread and want to repeat process in that thread. i had tried but it doesn't work.
my code snippet is given below
[NSThread detachNewThreadSelector:#selector(setupTimerThread) toTarget:self withObject:nil];
-(void)setupTimerThread
{
NSLog(#"inside setup timer thread");
// self.startTiemr = [[NSTimer alloc]init];
startTiemr = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(triggerTimer) userInfo:nil repeats:YES];
runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:startTiemr forMode:NSRunLoopCommonModes];
[runLoop run];
}
can anyone suggest me a proper way to do that?
Thanks in advance.
#import "TimePassViewController.h"
#interface TimePassViewController ()
#end
#implementation TimePassViewController
{
NSTimer *timer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [self performSelectorOnMainThread:#selector(yourmethod) withObject:nil waitUntilDone:NO];
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:#selector(yourmethod)
object:nil];
[myThread start]; // Actually create the thread
}
-(void)yourmethod
{
#autoreleasepool
{
NSRunLoop *TimerRunLoop = [NSRunLoop currentRunLoop];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerMethod) userInfo:nil repeats:YES];
[TimerRunLoop run];
}
//timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerMethod) userInfo:nil repeats:YES];
}
-(void)timerMethod
{
NSLog(#"aaaaaaaaaaaa");
//[timer invalidate];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
try this code:
NStimer *uptimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(bachground_todaylist) userInfo:nil repeats:YES];
-(void)bachground_todaylist
{
[self performSelectorInBackground:#selector(yourmethod) withObject:nil];
}
you wants to stop proces
[uptimer invalidate];
I am using
[NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
I want to stop calling this timer one.
viewDidDisappear
How can i do that? invalidate?
Declare NSTimer *myTimer in .h file.
Assign instance as tom said like this
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
Stop and Invalidate using this
- (void) viewDidDisappear:(BOOL)animated
{
[myTimer invalidate];
myTimer = nil;
}
Try this
viewController .h
NSTimer *timer;
viewcontroller.m
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(pollTime)
userInfo:nil
repeats:YES];
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[timer invalidate];
}
Yes, you would use the - (void)invalidate instance method of NSTimer.
Of course, to do that, you would have to save the NSTimer instance returned from [NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:] into an ivar or property of your view controller, so you can access it in viewDidDisappear.
invalidate Method of NSTimer is use for stop timer
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
If you are not ARC then don't forget [self.timer release];
For stopping or invalidating NSTimer first you have to create instance for NSTimer in Globally.
Timer=[NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
after that try like this,
if (Timer != nil) {
[Timer invalidate];
Timer = nil;
}
I want to start a timer when a viewWillAppear method gets called.
It's working perfectly, but I'm having a problem when I push a new view on this Current View, which already has a timer. When I pop that new View and recall viewWillAppear I want to start the timer again as before. I have most of the techniques but can't find a solution.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[NSThread detachNewThreadSelector:#selector(createTimer) toTarget:self withObject:self];
}
- (void)createTimer
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
gameTimer = [[NSTimer timerWithTimeInterval:1.00 target:self selector:#selector(timerFired:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:gameTimer forMode:NSDefaultRunLoopMode];
timeCount = 360;
[runLoop run];
[pool release];
}
- (void)timerFired:(NSTimer *)timer
{
if(timeCount == 0)
{
[self timerExpired];
} else {
timeCount--;
if(timeCount == 0) {
[timer invalidate];
[self timerExpired];
}
}
timeLabel.text = [NSString stringWithFormat:#"%02d:%02d",timeCount/60, timeCount % 60];
}
- (void) timerExpired
{
//NSLog(#"Final == %#",arrayAnswers);
//NSLog(#"Attempt == %d",[arrayAnswers count]);
[gameTimer invalidate];
gameTimer = nil;
}
Well, based on experience, your problem is the timer's thread. Basically, the problem is here:
[NSThread detachNewThreadSelector:#selector(createTimer) toTarget:self withObject:self];
I'm not absolutely sure but I believe that your code is not really wrong. I haven't figured it out yet but I guess the NSTimer object is not being activated when detachNewThreadSelector: is used. Try executing the timer on the main thread.
[self performSelectorOnMainThread:#selector(createTimer) withObject:nil waitUntilDone:NO]
I've done this and got the results that I wanted.
i added [self fbButtonClick:nil]; , but it does't call login screen. Why?
- (void)viewDidLoad {
_facebook = [[Facebook alloc] initWithAppId:kAppId];
[self.label setText:#"Please log in"];
_getUserInfoButton.hidden = YES;
_getPublicInfoButton.hidden = YES;
_publishButton.hidden = YES;
_uploadPhotoButton.hidden = YES;
_fbButton.isLoggedIn = NO;
[_fbButton updateImage];
[self fbButtonClick:nil];
}
You're suppose to call the authorize method on the _facebook object to login. That will call the login screen
i solved this problem with timer
NSTimer *timer = [[NSTimer timerWithTimeInterval:0.001
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:NO] retain];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
- (void)timerFired:(NSTimer *)timer
{
[timer invalidate];
[timer release];
timer = nil;
[self fbButtonClick:nil];
}
I am trying to run a NSTimer on a thread using iPhone SDK 3.0. I think I am doing everything correctly (new runloop etc.). If I call [timer invalidate] on viewDidDissappear though I get this error:
bool _WebTryThreadLock(bool), 0x3986d60: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
Program received signal: “EXC_BAD_ACCESS”.
Here is my code:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[activityIndicator startAnimating];
NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:#selector(timerStart) object:nil]; //Create a new thread
[timerThread start]; //start the thread
}
-(void)timerStart
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
//Fire timer every second to updated countdown and date/time
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(method) userInfo:nil repeats:YES] retain];
[runLoop run];
[pool release];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[timer invalidate];
}
When I remove the line invalidating the timer everything works fine. Am I not supposed to invalidate it or am I making some other mistake?
Thanks
Try
[timer performSelector:#selector(invalidate) onThread:timerThread withObject:nil waitUntilDone:NO];
instead. You will have to make timerThread an ivar of your view controller.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:#selector(timerStart) userInfo:nil repeats:TRUE];
[timerThread start];
}
-(void)timerStart
{
#autoreleasePool{
NSRunLoop *TimerRunLoop = [NSRunLoop currentRunLoop];
[NSTimer scheduleTimerWithInterval:0.1 target:self selector:#selector(methodName:) userInfo:nil repeat:YES];
[TimerRunLoop run];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
I guess you have done some UI work in "method".
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(method) userInfo:nil repeats:YES] retain];
From the log, it seems you have done UI updating work it in "Timer" thread in "method".
you can use block to dispatch work on main thread or performSeletorOnMainThread for doing "method"