show uiactivityview while an other operation is loading - iphone

i googled for hours now but i can't find a code example, which describes how to show an uiactivityview while an operation/method is loading.
in my case i want to show it while i load a large image to an uiimageview. have you got an idea how to realize that?
thanks in advance.
sean

You might want to look in the UI UIActivityIndicatorView Class Reference documentation.
Make sure to return to the run loop while waiting for the image to load in order to see the activity indicator animating. Also, you will probably need to create a method to stop the activity indicator, and perhaps call that stop method by doing a performSelectorOnMainThread from whatever method handles the asynchronous completion of the loading.

well its quit easy.
show activity indicator
start thread loading that image
if image loaded, thread tells mainapp to kill activity indicator.
NSThread detachNewThreadSelector:#selector(myMethod)
toTarget:self
withObject:nil];
and
[self performSelectorOnMainThread:#selector(myMethod)
withObject:nil
waitUntilDone:false];
will be helpfully.
taken from http://www.iphoneexamples.com/

Related

Displaying ActivityIndicator immediately

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];

UIActivityIndicator starting loop after the execution of the process

I am trying to get my UIActivityIndicator start animating when the process of data consumption start downloading. My concern is: how do you get the activity indicator start animating at the same time? I have gone through some solutions here on Stack Overflow, yet I am trying to find some universal solution, since I am utilizing a subclass of UIActivityIndicator. I am not sure if a separate thread can resolve this issue?
I used:
[self performSelector:#selector(methodname) withObject:nil afterDelay:0];
However, this didn't solve my problem.
Just return from the method starting the activity indicator. The UI (including the activity indicator) updates after an app returns to the main run loop. Handle any long activities (such as your download) asynchronously with delegates or callbacks, so that the activity indicator can continue to run. So return from the method that starts your download as well. Finish later in another method.
Another option is to do the download, or other long process, in another thread. Operation queues and blocks can be used for this purpose. Use a performselector on the main thread to update the UI, as an app can't update the UI directly from within a background thread.

how to cancel the background running thread in my iphone app

i wrote like:
[self performSelectorInBackground:#selector(backgroundImageLoading) withObject:nil];
to load image background but know i need to stop this when i refresh the view but i don't have any idea how to do
See NSOperation and NSOperationQueue.
Its also to do the process concurrently as like background thread. Here in NSOperation you have cancel method available to stop the operation.
The threads should not be forced to kill. look out the reasons here. It may help you to find the solution.

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.

Why does addSubview load the view asynchronously

I have a UIView that I want to load when the user clicks a button. There happens to be some data processing that happens as well after I call addSubview that involves parsing an XML file retrieved from the web. The problem is the view doesn't show up until after the data processing even if addSuview is called first. I think I'm missing something here, can anyone help?
Code: I have a "Loading..." view I'm adding as a custom modal (meaning I'm not using the modalViewController). This action is linked to a button in the navigationController.
- (IBAction)parseXml:(id)sender {
LoadingModalViewController *loadingModal = [[LoadingModalViewController alloc] initWithNibName:#"LoadingModalViewController" bundle:nil];
[navigationController.view addSubview:loadingModal.view];
[xmlParser parse];
}
Howdy! If you're looking for an easy work around:
[self showLoadingScreen]
[self performSelector:#selector(methodToDoWork) withObject:nil afterDelay:0.3];
However you're better off making methodToDoWork asynchronous if you can.
If you are doing your processing on the main thread, it will block the main thread until its done, which means your UI will become unresponsive and not update until the main thread resumes.
You need to perform your XML processing on a background thread using something like NSOperation or an existing asynchronous API and update your view when you have finished.
Its hard to be of more help and get a better idea of whats going wrong without seeing your code unfortunately.