Re: updating UI during long loop using cocoa objective-c
Re: updating UI during long loop using cocoa objective-c
- Subject: Re: updating UI during long loop using cocoa objective-c
- From: Fabio Mancinelli <email@hidden>
- Date: Mon, 2 Apr 2007 17:52:25 +0200
On Apr 1, 2007, at 5:26 AM, <email@hidden>
<email@hidden> wrote:
I want to update my UI after each iteration of a long loop in a
objective-c cocoa application.
for(i = 0; i < loopMax; i++)
{
// do lots of processing
[myTableView reloadData]; // also update progress indicators
}
I would solve your problem by refactoring the loop in the following way:
1) Encaspulate a single loop step in a method of a class that
contains all the variables needed for executing that single step.
Roughly speaking your variables i and loopmax would become instance
variables.
2) Setup a timer that calls repeatedly the method at a regular
(small) time interval.
3) The "step" method should be able to check when the processing is
over, so it can release the timer when it's done and notify whoever
is interested that the processing is over (for closing windows,
cleaning up, etc.)
With a bit of effort you could end-up with a good design for this
kind of things (à la NSAnimation, for example).
In this way you don't dive into multithreading and your code is
executed in the main thread leaving the UI responsive to other events.
See you,
Fabio
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden