mtompson
mtompson

Reputation: 261

NSString alloc or not!

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?

Upvotes: 3

Views: 1084

Answers (3)

Nick Moore
Nick Moore

Reputation: 15847

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.

Upvotes: 3

Tomas Vana
Tomas Vana

Reputation: 18775

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.

Upvotes: 2

Max
Max

Reputation: 16709

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.

Upvotes: 1

Related Questions