is it a good idea to use performSelector:withObject:afterDelay for animation purpose when UIView or CALayers are just not available?
More specifically I am working with MKOverlayRenderer which can display something like an UIView does. I'd like to animate a movement of this MKOverlayRenderer (just think of a progress bar). In my implementation I just repeatedly send performSelector:withObject:afterDelay to update the position of the MKOverlayRenderer in very short time intervals.
It works quite well but I am concerned about the performance since the CPU usage rises from ~0% to 15% (if I set the delay to 0.5 seconds) and 90% (delay = 0.02).
Are there are any other options or can I just ignore the CPU peaks?
I'm not sure of anything specifically for MKOverlayRenderer, but you can create an NSTimer like so:
#property (nonatomic, strong) NSTimer *myTimer;
And then instantiate it:
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:#selector(myMethod)
userinfo:nil
repeats:YES];
and then your method:
- (void)myMethod {
//do whatever MKOverlayRenderer animation you need
}
and then to stop the timer:
[self.myTimer invalidate];
self.myTimer = nil;
But just remember that the CPU overload is from the animation you're running, not the timer.
Related
I am trying to make a timer app. I am fine with Play button but I couldnt get Pause button working. I have seen some tutorials on Timer Apps and most of them have only used: [timer invalidate] code for that method that solely stops the time that is currently being shown in the label (display). Even that code doesn't work for me so I tried doing this which makes kinda more sense but still, of no luck.
#implementation ViewController
int timerCounter=0;
NSTimer *timer;
NSString *label;
BOOL isPaused=NO;
-(IB Action) playButton:(id)sender{
[timer invalidate];
isPaused=NO;
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector: #selector(tick) userInfo:nil repeats:YES];
}
-(IBAction) pauseButton:(id)sender{
[timer invalidate];
isPaused=YES;
label=[NSString stringWithFormat:#"%d",timerCounter];
_labelTimer.text=label;
}
-(void) tick{
if (isPaused==NO){
timerCounter++;
}
label=[NSString stringWithFormat:#"%d",timerCounter];
_labelTimer.text=label;
}
The NSTimer API do not have any method for pausing. What is available is either fire or invalidate. About your code, You are using global variables - not a good practice, most probably the instance of timer you are calling is not the same, remove them and add a property in the class extension in .m instead:
#property (nonatomic, strong) NSTimer * timer;
you then address that property with self.timer.
If this does not help, check if the button call the method when you press it.
My app adds a custom SKNode object to a SKScene at a fixed interval (sub second) using an NSTimer. At certain times I want the timer to speed up. Here's what I do (code simplified):
#interface MyScene : SKScene
#end
#implementation MyScene {
MyNode *node;
NSTimer *timer;
int speed;
}
- (id):initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
speed = 0.8f;
timer = [NSTimer = scheduledTimerWithTimeInterval:speed target:self selector:#selector(addNodeToScene) userInfo:nil repeats:YES];
}
}
- (id):addNodeToScene {
if (node != nil) {
[node removeFromParent];
node = nil;
}
CGPoint location = //random x y coordinates using arc4random()...
node = [[MyNode alloc] initAtLocation:location];
[self addChild:node];
}
// at some point I call this method (called regularly throughout life of app)
- (id):speedUp {
[timer invalidate];
timer = nil;
speed *= 0.9f;
timer = [NSTimer = scheduledTimerWithTimeInterval:speed target:self selector:#selector(addNodeToScene) userInfo:nil repeats:YES];
}
I've noticed a slight lag every time i call speedUp. I'm very new to Objective-C so not sure how to resolve. Is it more efficient to use dispatch queues over a timer or can I not avoid this issue because the internal is so fast?
I ran time profiling on instruments and this was not highlighted - instead my heaviest method was addNodeToScene.
My guess is that you're calling -speedUp outside of the normal work in -addNodeToScene. If so, the lag is likely coming from the fact that you've already used up a bit of the previous delay, then -speedUp invalidates the old timer and creates a new one, which starts the delay all over again. You'll see a lag any time you're more than 10% beyond the previous timer fire.
I'll try some ASCII art to illustrate. + is a timer fire, * is when you call -speedUp and it resets the timer.
+---------+--------+---------+---------+---------+
+---------+---*--------+--------+--------+--------+
Found the solution in another question: Xcode / Objective-C: Why is NSTimer sometimes slow/choppy?
(use CADisplayLink instead of NSTimer)
I'm working on a project, where i have to update the text of a UILabel really regularly (0.085f). So, I insert the update of the label in a loop like this :
MetresNumber = MetresNumber + 0.25;
DisplayMetres.text = [NSString stringWithFormat:#"%07.0f", MetresNumber];
I precise that "MetresNumber" is a float, and "DisplayMetres" the UILabel.
And this sort of code really really makes bad performances ! It's incredible how slower it goes since i've added those lines.
I made some searches, and found elements like :
[DisplayMetres setNeedsDisplay];
But it didn't change and update the text on the label.
It's in a loop called with :
timer = [NSTimer scheduledTimerWithTimeInterval:0.085 target:self selector:#selector(myLoop) userInfo:nil repeats:YES];
So my question is, could my code be improve, to get better performances, or should i forget my UILabel because it's too slow with ?
Thanks !
(void)setNeedsLayout
Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.
Another problem is that a scheduledTimer will not get called while the main thread is tracking touches. You need to schedule the timer in the main run loop.
So instead of doing
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateLabel:) userInfo:nil repeats:YES];
use
NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(updateLabel:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Also Use Timer selector method like below:
- (void) updateLabel:(id) sender {
MetresNumber = MetresNumber + 0.25;
NSString *text = [NSString stringWithFormat:#"%07.0f", MetresNumber];
dispatch_sync(dispatch_get_main_queue(), ^{
DisplayMetres.text = text;
});
}
There shouldn't be any performance issues when you are just updating a single label every 85ms. First find out what actually causes the lags. Use the Time Profiler instrument.
I think most time will be spent on the string drawing.
Here are some tips how you can optimize your code:
You don't need to call setNeedsLayout or setNeedsDisplay explicitely on the label. Just set the text property (on the main thread of course).
The resulting string in your code will always be a 7-digit integer. Consider using an integer instead of a float. Formatting an integer will be faster.
stringWithFormat can be slow sometimes. You could try using a NSNumberFormatter or simply generate the string with: [#(MetresNumber) stringValue]
In your code the string actually doesn't change everytime the timer fires. Only every 4th time. You can set the time interval to 4*0.085 and replace MetresNumber = MetresNumber + 0.25 with MetresNumber = MetresNumber + 1.
Try using this custom UILabel class
Don't use UILabel at all. Use pre-drawn images for each digit.
Schedule the timer with NSRunLoopCommonModes (see answer from Lightygalaxy)
When testing my app for memory leaks I discovered that whenever I start NSTimer with an interval, it shows that CFArray (store-deque) and CFArray (mutable-variable) keeps growing in size. In my actual app Malloc 16 and Malloc 32 etc increases in size alongside with the CFArray.
Question: how do I stop this "leak"?
code: .h
#interface ViewController : UIViewController
{
NSTimer *timerClock;
int timer;
}
#end
code: .m
- (void)viewDidLoad
{
[super viewDidLoad];
timer = 0;
timerClock = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(counter) userInfo:nil repeats:YES];
}
- (void)counter
{
}
#end
Are you seeing this when you dismiss your view controller? I ask this because a repeating NSTimer keeps a strong reference to its target and if you don't invalidate the timer when you dismiss the view controller, you'll leak both the timer and the controller itself because you will have a strong reference cycle (a.k.a. a retain cycle) between the controller and the NSTimer.
By the way don't try to invalidate in the controller's dealloc method, because with the strong reference cycle, dealloc will never get called. Often people will invalidate in viewDidDisappear. And, clearly, if you're going to invalidate your timer in viewDidDisappear, you probably should be creating it in viewDidAppear rather than viewDidLoad, to make sure you balance your creation of the timer with its invalidate calls.
I don't want to create NSTimer object. How do I invalidate timer? I want to invalidate timer in viewWillDisappear.
-(void) viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
}
A
you have to hold on to the timer you create:
#interface MONObject ()
#property (nonatomic, retain) NSTimer * timerIvar;
#end
#implementation MONObject
...
- (void)viewDidLoad
{
[super viewDidLoad];
self.timerIvar = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
}
- (void)invalidateTimer
{
[self.timerIvar invalidate];
self.timerIvar = nil;
}
- (void)viewWillDisappear:(BOOL)animated
{
...
[self invalidateTimer];
}
B
another option would be to invalidate the timer that is passed in the callback, but that won't occur within viewDidUnload:. therefore, it doesn't quite apply in this scenario:
- (void)onTimer:(NSTimer *)pTimer
{
[pTimer invalidate];
}
If you want to be able to cancel the timer, you have to refer to the timer you’re cancelling, and that means you have to keep the pointer to the timer around, see justin’s answer.
Keeping a reference to the timer is the right way to do it, but for the sake of completeness you may also use the -performSelector:withObject:afterDelay: method as a poor man’s timer. That call may be invalidated using +cancelPreviousPerformRequestsWithTarget:. Sample code:
- (void) viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(timerTick) withObject:nil afterDelay:10];
}
And then:
- (void) viewWillDisappear
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[super viewWillDisappear];
}
But this is not the right way to do it, because there might be other perform-selector requests pending on your object that you would cancel. It’s best to keep your timer around, that way you know exactly what you’re cancelling.
By the way, it’s also probably a bad idea to run a timer in -viewDidLoad. View loading may happen anytime, without any relation to view being displayed.
Maybe this method can help you:
[self performSelector:#selector(onTimer:) withObject:nil afterDelay:10];
If you don't want to hold on to your timer, the NSTimer object will be passed to the timer method (in your case onTimer:), so in that method you could check whether the timer is still needed and invalidate it. However, you will run into trouble if the view comes back before you invalidated the timer, and you create a new one.
By far the best way is to store the timer into an instance variable. It works, no clever tricks, and you'll know six months later what you did. I'd probably write a
#property (readwrite, nonatomic) BOOL hasTimer;
getter returns YES iff the timer is not nil, setter invalidates the timer or creates a new one.