tags:

views:

525

answers:

2

I'm making a simple iPhone app whose sole function is to update a UIView forever (until it exits).

I tried this in applicationDidFinishLaunching and viewDidLoad:

while(1) {  
    // update view here  
}

but that doesn't work- the app never finishes loading. I'm sure there's a simple solution to this, I just don't know what it its.

Also: ideally, this process should consume very little resources.

+3  A: 

You can't have a while (1) statement like there, as it not allow viewDidLoad to return, and your app will never get any other calls such as tap processing, screen draw updates, etc.

In viewDidLoad, set up a timer task using:

updateTimer = [NSTimer scheduledTimerWithTimeInterval: kUpdateTimeInterval target:self selector:@selector(updateView) userInfo:nil repeats:YES];

And have a method called updateView that actually does the updating. Instead of updateView you could also use setNeedsDisplay which will trigger a call the -drawRect method of your view class, and do the actual drawing there.

What ends up happening now is that your viewDidLoad will set up a repeating task and at every kUpdateInterval, your view will be updated.

mahboudz
Thanks- that was exactly what I was looking for :)
igul222
+1  A: 

Instead of updating the view all the time, maybe you could just call its -setNeedsDisplay method when the data that you're displaying changes.

Thomas Müller