I'm just wondering, is there a better, more efficient function to use than NSTimer? I have a NSTimer code like this on my ViewDidLoad function:
[NSTimer scheduledTimerWithTimeInterval:900.0f target:self selector:@selector(methodToRun) userInfo:nil repeats:YES];
With the function methodToRun:
-(void)methodToRun {
if(currentTime == presetTime) {
//do something
}
}
This works fine but the problem is, this eats up a lot of memory and I am getting memory warnings. So, what's a better, more efficient and less memory consuming way of triggering my methodToRun continuously to check if the currentTime is equal to presetTime?
You can use
dispatch_after
.An alternative solution to this can be achieved by using the dispatch_after method and a weak pointer to self.
With this you will have a scheduled task that will be called at every sec, it will not retain the target (self) and it will end itself if the target is deallocated.
Source:NSTimer alternative
Just run a while loop, and use
sleep()
orusleep()
functions at the end of the loop which will define the time interval.PS: don't do it on main thread
Edit: I see that I am getting downvotes, but can somebody tell me what is wrong with this approach?
Based on @Anoop's answer here is swift version:
And then simply start it with:
Better swift version using optional chaining and weak self