views:

2040

answers:

3

Hello guys,

I am working on a an application which is very simple a navigation controller with a table view when the user clicks a row, he is directed to the details view.

However, the details view pulls data from Core Data. i am pulling a relatively large amount of data that takes about three seconds to load.

I wanted to add that UIActivityIndicatorView to show progress.

I tried to start the animation once the user clicks the row, so i set it to animate in didSelectRowAtIndexPath

For some reason, the Activity Indicator doesn't start before the pushing of the details view.

Any idea why? or the best way to implement such an idea?

~Adham

+2  A: 

Because you start the animation and then start a large operation in the same thread. Consider running that 3 second operation in a new thread. Look at NSOperationQueue and then create a NSOperation to run that procedure. It will work this way.

marcc
Putting the Core Data fetch into its own thread will free up the UI to start and stop the activity indicator. I definitely recommend either using `NSOperation` and `NSOperationQueue` for the fetch operations, or instantiating a separate thread for the fetch.
Alex Reynolds
Thanks for answering..But can you please give me a link to a tutorial on using this? all the pages i found so far are very complicated.I will use this Queue only once for fetching my record.
Adhamox
A: 

The UI doesn't update until the end of your run loop. You are, in sequence, displaying the activity monitor, then pushing the new table view, and then the UI updates. You need to change this order.

You can either move something to a different thread, or you could perhaps delay the loading of the new table view by calling performSelector:afterDelay: with a delay of 0. That will delay the loading of the new table view until after the activity indicator appears in the UI. Now, it's still all on the same thread, so you will be blocked from doing anything, but if the animation is threaded in the activity monitor, it would make for a quick and easy solution.

Matt B.
A: 

Call method in thread:

[NSThread detachNewThreadSelector: @selector(loadMethod) toTarget:self withObject:nil];

See following for more details:
http://iphone.zcentric.com/?s=UIActivityIndicatorView

Brij