3

I am running this code from the scrollViewDidScroll method (so it runs when you scroll!):

NSString *yearCount = [[NSString alloc] initWithFormat:@"%0.1f", theScroller.contentOffset.y];  
years.text = yearCount; 
[yearCount release];

which works fine, however it hits performance on the scroll (causing it to judder as it slows down)

My question is, do I need to keep using alloc and release or is there a way to get some numbers using initWithFormat onto some text without it?

hoha
  • 4,418
  • 17
  • 15
mtompson
  • 261
  • 3
  • 21

3 Answers3

3
years.text = [NSString stringWithFormat:@"%0.1f", theScroller.contentOffset.y];

will avoid the need to explicitly release the string, since it is autoreleased.

However, if you are trying to avoid slowdown, consider updating the field less frequently. For example, each time scrollViewDidScroll is called, set a timer to update the field in say 0.1 seconds from now, but not if the timer is already running from a previous call. This reduces the number of calls while keeping the UI updated.


Here is an example how you could do it. Declare an NSTimer in the interface declaration of your scroll view delegate:

NSTimer *timer;

And methods:

- (void)updateYear:(NSTimer*)theTimer
{
    timer=nil;
    UIScrollView *theScroller=[theTimer userInfo];
    years.text=[NSString stringWithFormat:@"%0.1f", theScroller.contentOffset.y];
}

- (void)scrollViewDidScroll:(UIScrollView *)theScroller
{
    if (!timer) {
        timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateYear:) userInfo:theScroller repeats:NO];
    }
}

Obviously, you don't have to use 0.1 as the time interval, you can try making it faster or slower and see what works best.

Note that this example is complete as far as memory management is concerned, You should not try to retain or release the timer object yourself. Its lifetime is handled internally by the runloop.

Nick Moore
  • 15,547
  • 6
  • 61
  • 83
  • 1
    This is just a convenience method that allocs the string behind the scenes. It is not an alternative. – Chuck Mar 16 '11 at 09:04
  • I have now added a more full answer. – Nick Moore Mar 16 '11 at 09:08
  • Any nice Timer examples (links) would be a great help.. (I will look as well, I'm not being lazy lol) – mtompson Mar 16 '11 at 09:22
  • as far as memory management goes would it just be 'timer' being released in the - (void)dealloc ? – mtompson Mar 16 '11 at 11:07
  • @Mark You should not release the timer yourself, since you did not not `alloc`, `retain` or `copy` it. Each timer instance is retained internally by the runloop, and gets released automatically when the timer is invalidated, which occurs after it fires (see the NSTimer class reference: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html). The example is complete as far as memory management goes. – Nick Moore Mar 16 '11 at 14:03
2

Consider using scrollViewDidEndDecelerating method to avoid the frequent updates. Alloc-init is not responsible for the performance decrease, setting the text frequently is. Unless you really need to change it continuously (in which case solution with a timer might be an option), you should be looking for a different hook method.

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
1

You've got poor performance absolutely not because of the string formatting or alloc-release. You can use some shorter form like:

years.text = [NSString stringWithFormat:@"%0.1f", theScroller.contentOffset.y];

which is equivalent to

years.text = [[[NSString alloc] initWithFormat:@"%0.1f", theScroller.contentOffset.y] autorelease];

However this won't help to improve your performance at all.

Max
  • 16,679
  • 4
  • 44
  • 57
  • why bother with the alloc version at all! ? – mtompson Mar 16 '11 at 11:21
  • @Mark sometimes you might not want to autorelease the string in which case you would `alloc` it but not `autorelease`. So `alloc`/`release` gives you more fine control but `stringWithFormat` is more convenient for when you would be autoreleasing the string anyway. – Nick Moore Mar 16 '11 at 14:12