Custom Activity Indicator Issue - iphone

I am making an iPhone app in which I have made a custom activity indicator class to show an activity indicator animating on another class, but my problem is that the activity indicator class get start animating but it doesn't get stop animating.
Here is the code:
customActivityView = [[ CustomActivityIndicatorView alloc] init];
customActivityView.view.center = self.view.center;
[self.view addSubview:customActivityView.view];
[customActivityView.activityIndicator startAnimating]; // is work fine to add a custom activity indicator and start animating
[customActivityView.activityIndicator stopAnimating]; // but this line doesn't work

Check that customActivityView.activityIndicator actually returns the activity view, or quite likely, if it returns anything at all.

You cannot start and stop an activity indicator in the same method.if you do so you cannot see anythng in result.

Sorry it's working now actually my custom Activity class was get assign two times that why it didn't stop.

Related

show activity indicator while loading ViewController

I have a ViewController that takes time to load its views. When I run Instruments, I see from the home screen, if I tap on the icon that pushes that view controller onto the stack, it's half laying out the views, and half getting the data for the views. I tried adding an activity indicator to display on the home screen over the button when the button is pressed to push the LongRunningViewController onto the stack. So I basically do this:
- (IBAction)puzzleView:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator startAnimating];
});
PuzzleViewController *detailViewController = [[[PuzzleViewController alloc] init] autorelease];
[self.navigationController pushViewController:detailViewController animated:YES];
[self.activityIndicator stopAnimating];
}
The home screen lags while pressing the button, and then loads the view. I'm trying to show the activity indicator while the other screen is preparing to be pushed (or at least that's the way I think it works). The activity indicator does not show however. I'm wondering if this can be done? Or do other apps push their ViewController, and then on that screen, they have the activity indicators showing the loading of their different resources?
When you say that it takes time to get the data for the views, I assume you mean that the PuzzleViewController does something non-trivial somewhere like viewDidLoad.
So let's say it is in viewDidLoad. Then you can do this:
-(void)viewDidLoad
{
[self.activityIndicator startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
//do something expensive
[self doSomethingExpensive];
//dispatch back to the main (UI) thread to stop the activity indicator
dispatch_async(dispatch_get_main_queue(), ^
{
[self.activityIndicator stopAnimating];
});
});
}
This will mean that the expensive operation will be put on a background thread and won't block the loading/showing of the view controller.
This approach is showing the requested view immediately when the user pushes the button, and showing progress on that screen. I think it's more common this way than to show an activity view while loading contents before presenting the view. This also buys you a little bit more time; your long operation can be running in the background during the transition animation!
The reason your activity view isn't showing the way you're doing it is that you're doing it all on the UI thread; using dispatch_async to the main queue from the main queue won't accomplish anything because your block won't have a chance to run until the run loop completes.

Activity Indicator not showing up

I have two issues with activity indicator:
1. Activity Indicator not showing up on UIViewController
I have activity indicator added in .xib file. On button click it should start animating. and when response from server is received, before going to next page it should stop animating.
I am doing it as follows:
activityIndicator.hidden = NO;
[activityIndicator performSelector:#selector(startAnimating) withObject:nil afterDelay:0.1];
[self.view bringSubviewToFront:activityIndicator];
....rest of code here....
activityIndicator.hidden = YES;
[activityIndicator stopAnimating];
Activity Indicator not showing up on UITableView
For table view I am doing it same way but on didselectrowatindexpath...
For tableview I also tried adding activity view to cell accessory, but still not showing up
In both cases activity Indicator is not showing up.
Please help
Thanks
If all this code is in one method or in response to one event, then none of the changes to the views are going be visible until you return to the event loop. You set the activityIndicator.hidden to NO and then set it again to YES before the UI has an opportunity to even refresh.
You also apparently stop the animation before you start it.
What you need to do is make the activity indicator visible here and start its animation. Then schedule the work to be done (start an asynchronous network connection, or put some work into a queue, or whatever it is you need to get done) and return from this method so that the UI can refresh, the indicator can be drawn, and the animation can actually start.
Then later at some point after the work is complete, you can hide the indicator and stop the animation. But you can't do all of that on the main thread within one single turn of the event loop. None of your changes will be visible because no drawing at all will happen here while this method is executing (assuming this is on the main thread)
I hope that makes sense?
Now I modified the code to this:
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
[self performSelector:#selector(saveClicked) withObject:nil afterDelay:0.1];
[self.view bringSubviewToFront:activityIndicator];
and it worked :)
May be, in tableView, instead of self.view , it will be self.navigationController.view ??

Activity indicator in UITableView in iphone app

In my iphone app I have a table view. In that onclick of each row it will download some data. During that download process I need to animate an activity indicator. How to add an activity indicator in table view?
First, you do not need to add activity indicator in table view. You just add it in that view and then set its hidden property. When the user clicks on a row, set its hidden property to NO.
One more thing, move your table to send to back from layout->send to back
and the indicator to send to front layout->sent to front.
Just add an UIActivityIndicatorView to your view say activity Assign it the frame and style you want.
Then all you need to do is that put the below code above your download code.
[NSThread detachNewThreadSelector:#selector(myMethod:) toTarget:self withObject:nil];
and you need to declare myMethod as
-(void)myMethod:(id)data
{
[activity startAnimating];
[activity setHidden:NO];
}
Whenever your download finishes just put
[activity stopAnimating];
[activity setHidden:YES];
Hope this helps you.

Yet another question about showing UIActivityIndicator

I had the UIActivityIndicatorView working fine in simulator and other 3.0 devices in my app. But I found out that it was not spinning (or showing) in the new iphone 4. Basically I need to show the activity indicator when a button is clicked and hide it when the button click event is complete. I was using the approach below.
[NSThread detachNewThreadSelector: #selector(spinBegin) toTarget:self withObject:nil];
from this link. As mentioned, it correctly spins the activity indicator on all except 4.*.. not sure why. To get around this, I also followed another approach something like (from developer.apple.com)
`
(IBAction)syncOnThreadAction:(id)sender
{
[self willStartJob];
[self performSelectorInBackground:
#selector(inThreadStartDoJob:)
withObject:theJobToDo
];
}
(void)inThreadStartDoJob:(id)theJobToDo
{
NSAutoreleasePool * pool;
NSString * status;
pool = [[NSAutoreleasePool alloc] init];
assert(pool != nil);
status = [... do long running job specified by theJobToDo ...]
[self performSelectorOnMainThread:
#selector(didStopJobWithStatus:)
withObject:status
waitUntilDone:NO
];
[pool drain];
}
`
The problem with this was that, it is showing the acitivityVIewIndicator spinning correctly (at least on the simulator) but after it stops, the built in activity indicator in the top bar (where it shows the battery% etc) is still spinning.
I'm new to objective C. I have finished my app completely but for this silly thing. I realize there is no way to display UIActivityView without starting another thread. and finally, just to rant, I don't understand why they have to make it so complicated. I mean they knew it was going to have this problem, why not provide a sample code everyone can use rather than deriving their own solutions.
Finally, can anyone please provide me with a direction or some sample code. I would really appreciate it. I have been searching for a few hours now and have not found anything really that works!
Why are you starting/stopping the indicator on a separate thread? Any methods you send to your UIActivityIndicatorView must be sent on the main (UI) thread.
Any events sent by a button pressed will automatically be run on the main thread. If you're using background threads to complete the process, you could do something like:
- (IBAction)buttonPressed:(id)sender {
// This runs on the main thread
[activityIndicator startAnimating];
[self performSelectorInBackground:#selector(inThreadStartDoJob:) withObject:theJobToDo];
}
- (void)inThreadStartDoJob:(id)theJobToDo {
// Set up autorelease pool
...
// Run your long-running action
...
// Stop the spinner. Since we're in a background thread,
// we need to push this to the UI Thread
[activityIndicator performSelectorOnMainThread:#selector(stopAnimating) withObject:nil waitUntilDone:YES];
}
Edit: As for the activity indicator in the top bar (where the battery is), doesn't this automatically start/stop based on network activity?

How to solve UIThread bloack issue in iPhone

I am creating an iPhone application where I need to show a login screen for few minutes, hence I created the custom view and added to the custom view controller which is added to the window for display. Now at the same time I need to check for some background database so, I am creating that in separate delegate and while after database operation is in finished it gives an callback to the main thread to display the new screen. But the first view is never getting displayed and my application directly lands up in the new view.
Please find below my code snippet:
(void)CheckForExistingData : (DatabaseSource *)theDatabaseConnection
{
BOOL isRecordExist = theDatabaseConnection.isrecordExist;
// Release the connection....
[theDatabaseConnection release];
theDatabaseConnection = nil;
if (isRecordExist == FALSE)
{
textLabel.text = #"Preparing the application for first time use, please wait....";
[activityIndicator startAnimating];
[self setNeedsDisplay];
}
else
{
// Now all categories are successfully downloaded, launch the category screen...
sleep(2); // sleep for 1 second to allow to show the splash screen....
[self.viewController LaunchCategoryViewController:self];
}
}
Here CheckForExistingData is an callback mechanism which will be called from the other thread.
You need to exit your method to see anything displayed. Not sleep or wait on a synchronous network call.
That probably means you need to break your sequential code into multiple methods, the subsequent parts being called by a splash wait timer, the view button handler, or the async network activity completion callback.
sleep() blocks your main thread, thus the UI has no chance to update.
But you can always send messages delayed. In your case, it would look like this:
[self.viewController performSelector:#selector(LaunchCategoryViewController:) withObject:self afterDelay:2.0];