views:

29

answers:

2

Hi,

Is there a way to update UI manually? For example, I have a function which updates UI and execute some logic. After the UI update, it will execute some logic that will take a long time and update of UI has to be wait until the execution of logic is finished. Is there a way to update UI manually befor even the logic is even executed?

It seems that thread can be used in here. But Is there a way to solve this by not using thread? Also, using if thread can be used, what is the best practice?

Thanks!

A: 

Are you thinking about a simple progress bar? If that is the case, then you can use something using NSApp (see the section under Managing the Event Loop) and use runModalForWindow: and runModalSession: These will allow you to open a progress panel, report on status and allow a method to cancel the operation.

Because the operation is modal, other UI elements will be deactivated until the panel is dismissed.

Steven Noyes
I am talking about UIkit from cocoa touch layer here.not appkit later from foundation layer...
David Gao
A: 

Because the UI thread is the main thread of your app, it's generally not a good idea to process big operations on it because you froze your UI in the meantime (which is not user friendly).

The way you use thread depends on what you want to do, you can for example just use - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg or you can create your own thread and be more specific.

Don't forget to call - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait if you want to do some changes on the UI from other thread.

You'll find all you need about thread programming in this guide from iOS Reference Library.

Hope this helps !

Ermiar
Thanks for the reply Ermiar.This updating problem actually happens a lot when you need to update a UI and then another UI but not at the same time.Let me give you an example.Suppose I want to dismiss one controller, call this controller 1, and then dismiss another controller right a way, call this controller 2. Controller2's parent is controller1, and controller1's parent is a controller.Then this problem will happen. it will actually dismiss the first controller but it won't dismiss second controller.Do you think this case, we don't have any other choice but to create a new thread?
David Gao
Also I am using a very hacky way to solve this problem.I am using NSTimer to dismiss controller1.Just curious, when NSTimer is being used, does it create another thread?
David Gao
It depends on how you initialize your timer, if you use scheduledTimerWithTimeInterval, the timer will be created in the current loop in the current thread. Otherwise you must add the timer in a run loop in other thread.You can use other methods to perform action in current thread after a delay like performSelector:withObject:afterDelay: if you want just wait for something.
Ermiar
thx ermiar for the help!i guess there is no other but to use some sort of delay, which is a hacky way...
David Gao