views:

59

answers:

2

I want to start an activity indicator before going to disk I/O.

How do I start the indicator NOW, instead of waiting for the next display loop cycle? Or how do I force the display loop before beginning the disk I/O?

Dan

A: 

You can't force the indicator to start now as far as I'm aware. To push a method call so that it's the next thing in the runloop, you can use:

[self performSelector:@selector(taskToDo) withObject:nil afterDelay:0]

Which will schedule 'taskToDo' (optionally with a single argument) onto the current runloop, to occur as soon as it can. Then just let that segment of code exit. An equivalent method is to schedule an NSTimer, but syntactically that's a bit more lengthy.

Tommy
Thanks. I tried performSelector but it doesn't change anything.
Dan Selig
performSelector:withObject: behaves exactly like a direct call in this sense. performSelector:withObject:afterDelay: schedules something for later. Did you try the latter?
Tommy
Hi Tommy, Yes I have. Please see my response to Mo.
Dan Selig
I apologize for wasting your time. performSelector does work. I had a coding error. - Dan
Dan Selig
+1  A: 

If I understand correctly your IO operation takes some time, and you want the UI to update as you're performing it, right? If that's the case, you have to move your operation to a background thread for the UI to have a chance to update itself, there's no workaround. use performSelectorInBackground:withObject: to call your IO operation, use NSOperation, or blocks if you're targeting iOS 4.0+

Mo
These are good ides. Let me explain more fully. Usually when I have a complex view to display, I start the attention indicator and then I addSubview or setNeedsDisplay with a performSelector delay. However in this case, I need to read an XML file and build a large data structure in order to display the view. So I cannot run in the background.
Dan Selig
So read the XML file and build your large data structure in a background thread, while the progress indicator spins in the main thread.
Daniel Dickison
Thanks Daniel. This doesn't change anything though. I can't run in the background because the new view cannot be displayed until the structure is built. What am I missing?
Dan Selig
I apologize for wasting your time. performSelector does work. I had a coding error. - Dan
Dan Selig