Displaying ActivityIndicator immediately - iphone

I know how to animate,display the Activity Indicator.
But I want to know how to immediately show the Activity Indicator.
Now When I am click the button it will load another view after 5 or many seconds. Clicking that button is loading a subview. When that Button Click method is called, it will call more than 7 methods after that and then it will load the subview. But the ActivityIndicator is displayed only after it complete executing all the 7 methods.
What I am trying to do is , I want to display the ActivityIndicator immediately after that Button Click method.
Any Idea ?

-(IBAction)button_click{
..
..
..
[self performSelector:#selector(afterDelay) withObject:nil afterDelay:0.3];
}
-(void)afterDelay{
YOUR_Code
}

The problem is that you perform your massive calculations (or loading from the internet?) on the main thread, but all the view mutation operations are not called immediately — on the start of the runloop the framework creates a new implicit Core Animation transaction, then it collects all the information about views' mutations, then commits the transaction on the end of the runloop. By blocking the main thread you are not allowing the transaction to commit and start your indicator's animation.
You can read about this architecture in the documentation: Core Animation Programming Guide: Transactions.
There are three options:
(Preferred) Perform your operations on the background.
Link your binary against QuartzCore.framework, then #import <QuartzCore/QuartzCore.h> and call [CATransaction commit] after your startAnimating call. This way you commit the implicit transaction.
Create your own explicit CATransaction.

If you have put the code to display or unhide the activityIndicator but it is not shown you have to use performSelector:withObject:afterDelay: method for doing any thing after unhide or displaying the activityIndicator with the delay of 0.001 will show the indicator immediately after clicking the button
Happy Coding :)

If your are talking about the system activity indicator, use this :
[[UIApplication sharedApplication] performSelector:#selector(setNetworkActivityIndicatorVisible:) withObject:[NSNumber numberWithBool:YES ] afterDelay:0.0];

Related

iPhone: Hide/Show controls under response

I am trying to show and hide three controls like UIBUtton, UILabel etc. in some scenario. I am doing this using below two functions.
- (void) hide
{
usernameField.hidden=YES;
passwordField.hidden=YES;
myLabel.hidden=YES;
}
- (void) show
{
usernameFieldField.hidden=NO;
passwordField.hidden=NO;
myLabel.hidden=NO;
}
But, when i call these functions under some server response code, that is i'm trying to show under connectionDidFinishLoading in success/failure server response..Its not doing that. i.e. Its not showing or hiding these controls in these situations..But same time, if i call these functions under a button click, its showing/hiding controls..So, Would these functions be called only under some events like button click? Can't we call from anywhere like i'm trying to do?
Please advise!
in your connectionDidFinishLoading
[self performSelectorOnMainThread:#selector(show)
withObject:nil
waitUntilDone:wait];
the reason is UI update show be called on main thread in order to get redraw.
Changing the hidden state of a control will not immediately draw the change to the screen, they will only flag it as needing to be drawn at some point in the future.
If the main thread is busy, then they will not get a chance to draw themselves.
Is your NSURLConnection code running on the main thread? You should move it to a background thread (but beware, changing the hidden property must be done on the main thread, not on a background thread! Look up grand central dispatch.)

Button image change slow

I am dealing with and odd bug, I have created a custom segmented control that comprises of uibuttons (the controller itself is a subclass of UIView. I change the button's images for the selected and normal control state to indicate selection.
In my parent view in IB I have set the identity of the view to my custom class. The issue is I have a tableView, when I scroll it and the scroll animation the button inside my custom view does not change its image immediately, instead it waits for the table to finish scrolling and then it updates. Any ideas?
You're not giving a lot of detail, but it sounds as if you're trying to do too much work within the current run loop. If so then the answer will be relevant:
iOS waits until your code has finished executing before it does any display updating. So any updates are, in effect, queued until your current chunk of code is completed. To get around this, the most common trick is to allow the current run loop to end and the pick up execution again after a very short delay.
So, in your case, call the code to update your custom segment control. And then, instead of calling the code to update your table, park that code in another method and call that method using [self performSelector: #selector(delayedUpdate) withObject: nil afterDelay:0.1];
To illustrate how you might change code:
BEFORE
[self updateSegmentController];
[self updateTableScrollPosition];
return;
AFTER
[self updateSegmentController];
[self performSelector: #selector(updateTableScrollPosition) withObject: nil afterDelay: 0.1];
return;
Often a delay of 0.0 works just fine; it still achieves the effect of letting the current run loop complete and the display update before calling the nominated method. Sometimes adding an extra delay improves your animation appearance.

Update UIProgressView from background

I have a tab bar application with three tabs. In my second tab view i have a UIProgressView. I have a NSTimer in my viewDidLoad method which calls a method which updates the progressView
progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:#selector(updateProgress) userInfo:nil repeats:YES];
All is fine so far.
I want to know how do i run the timer in the background throughout the lifecycle of the app, so that progressView is updated no matter which view the user is in. It will be great if you can show me with a code snippet.
Thanks in advance.
It's not advisable to update UI elements from background threads. Also, it's not advisable to modify UI elements of a view when the user is not in that view. You are using precious system resources.
Better way is to update the ProgressBar as soon as that view becomes active...
UPDATE: You can see here to know how to run a task in background thread. You could set up your NSTimer to start in the selector you specify there. But again, this kind of stuff could lead to some weird bugs. Better to avoid..
You could keep a property in the ProgressViewController (or even in the app delegate), say a float that tracks the progress. When you need to update the progress, another ViewController can change the value of the property.
Once the view becomes visible, update the UI (UIProgressView) from viewWillAppear:.
u can add the method in background this way
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
// increment the progress here and keep the property of that count in appdelegate
//
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
});
});
in diffent view's viewWillAppear u can set this way:
[loadingProgressView setProgress: appDelegate.count];

is this code using UIActivityIndicatorView flawed?

Is this code using UIActivityIndicatorView flawed? It appears that I don't actually get to see the indicator/spinner at all here, so is this because the view isn't drawn until the who viewDidLoad completes?
Is the only way around this to do the viewDidLoad custom work (e.g. data updates) on a separate thread? (I was hoping in this case for an easier single-thread operation). Is there a way to force the view to refresh after the "startAnimating" line perhaps prior to the data loading commencment?
Code from UITableViewController implementation:
- (void)viewDidLoad {
// Wait indicator - Start
self.waitView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
self.waitView.hidesWhenStopped = true;
[self.view addSubview: self.waitView];
// Load data into tableview
[NSThread sleepForTimeInterval: 5.0]; // Test code to simulate
[self.waitView stopAnimating];
}
You should also call startAnimating. Sleeping is not a good idea. I would prefer the performSelector-methods which starts a not recurring NSTimer under the hood.
Try this:
-(void) doStuff:(id)aSender
{
[self.waitView stopAnimating];
}
-(void)viewDidLoad
{
...
[self performSelector:#selector(doStuff:) withObject:self afterDelay:5.0];
}
in addtion: also set the frame- or bounds-property of the ActivityIndicatorView somewhere like sosborn said in his comment
Actually the answer from Thomas should work as it is, I will add a little explanation as to why not use sleep as you have done it.
All the UI processing on iPhone (and most of OSs as well) is being done in only one thread - the main thread, the thread that executes the so called run loop. If you stop that thread the UI will stop, nothing will be drawn.
Putting sleep into viewDidLoad, which runs in the main thread, will do just that - stop UI from doing anything. So because immediately after wakeup you've called [self.waitView stopAnimating] and the activityview should hide when not animating, you can't see it at all - you just didn't give it any time to show.
Thomas used a NSTimer to call stopAnimating after 5 seconds - now this lets the main thread to execute code before stopping animation and hiding waitView and this will work for your test.
Better yet you just let it animate without any timer and use a delegate patter to be informed by the tableView loading code after the data has been loaded, then stop animating. You don't know how long loading of data will last, so it's better to wait until it's finished than stop animating after any specific time.
Oh well, and the size and position, makes sense, but for testing it doesn't matter and is not the cause of not seeing it - if not specified it will be added at 0,0 and have a default size so you will see it anyway.

UIActivityIndicatorView with UITableView in Navigation Controller

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
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.
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.
Call method in thread:
[NSThread detachNewThreadSelector: #selector(loadMethod) toTarget:self withObject:nil];
See following for more details:
http://iphone.zcentric.com/?s=UIActivityIndicatorView