Moin Shirazi Moin Shirazi - 3 months ago 14
iOS Question

Invalidate NSTimer after VC is changed

I am having a

NSTimer
in my VC and its working fine its sending location of user and i cant invalidate it on
viewDidDisappear
as i need to send in background also.

myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0 target: self
selector: @selector(sendDataToSocket) userInfo: nil repeats: YES];


But the issue is when i instantiate same VC again it start the
NSTimer
again and 2 timers are working. so how can i stop the previous one or any other solution

Any help will be appreciated
Thanks

Answer

You can use the singleton class or simple use the appDelegate class to just declare the myTimer object in appDelegate and use it in your viewController.

This will have a single object of timer and you dont need to create multiple objects and your ultimate objective will be resolved. Hope this Helps!

Comments