Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top