Iphone – NSThread plus tickertape animation

iphone

I have a question that we might answer together i have a tickertape in my iphone app (Like those stick tickers) and i use a NSThread to keep the memory away from the main thread so it will not slow down the app. Now the thing is it does its job well but when i scroll on a UITableView that i have on the same view i notice that my ticker tape animation stops to work.

ViewController.m (Main view of this object has the ticker tape on it)

-(void)startTicker {
  [NSThread detachNewThreadSelector:@selector(start) toTarget:ticker withObject:nil];
}

TickerView.c (This handles the tickertape animation)

// Called from the viewcontroller

-(void) start {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self performSelectorOnMainThread:@selector(loop) withObject:nil waitUntilDone:YES];
        [pool release];
}

-(void)loop {
timerHandle = [NSTimer scheduledTimerWithTimeInterval:.01f target:self selector:@selector(render) userInfo:nil repeats:YES];
}

-(void) render {
   // Does a *** load of calculations here and moves the items in the tickertape..
}

My Question: How can i prevent the UITableview or any other view / touch event to block this thread from updating the tickertape animation ?.

Best Answer

Your NSTimer is not running on a background thread, but on the main thread. It will block anytime something else runs on the main thread. -performSelectorOnMainThread: means that anything done within the method called will run on the main thread.

To make your loop truly independent of the main thread, you could set up a while loop within your start method that sleeps for a given interval on every pass, then calls your render method. You'd need to make sure that all user interface updates within your render method get performed on the main thread, but make waitUntilDone NO for those method calls. I've also done this using NSOperations, where as one operation finishes I add another to the queue.

Also, running this render operation 100 times per second is excessive. I'd back that down a bit, or even better, look at using Core Animation for your ticker to make your drawing more efficient.

Related Topic