Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hello,
I have an app which needs some data initially and this takes som e time to load. To inform the user, I thought I could load something in the init method and then in the viewDidLoad call the actual nobfiel to be loaded. But it does not work. My approach is:

Code:
-(id) initWithTabBar {
	if ([self init]) {
		//add sometiing to tell the user - ideally a activation indicator or a label...
	}
	return self;	
}


- (void)viewDidLoad {
//remove the indicator and load the actual nibfile like:
[super viewDidLoad];
[[NSBundle mainBundle] loadNibNamed:@"myNibFile" owner:self options:nil];
}

It is not neccesary to show this indication more than once when the data is read, ie on init.

Any tips?
thanks in advance!
MACloop
 
The uiactivityindicator requires to be called in a method that is before the data loads in order to start the animation. Then once the data loads you need to stop the animation. The way you set it up in your sample code, look at the uiactivityindicator. You can call the start animation in the init. and stop it at the end of the viewdidload.
 
The uiactivityindicator requires to be called in a method that is before the data loads in order to start the animation. Then once the data loads you need to stop the animation. The way you set it up in your sample code, look at the uiactivityindicator. You can call the start animation in the init. and stop it at the end of the viewdidload.

Thanks for your answer. I tried that but the peoblem is...I think I did not mention that - I have a tabbar and all my viewControllers are activated by clicking the buttons on this tabbar. I have tried to make both my appDelegate, and the viewcontrollers self to be a tabBarDelegate and tabBarcontrollerDelegate to detect if a button is pressed but it does not seem to work. The way you suggested is the closest I have come, but the problem is that the indicator only shows upp when the "new view" is displayed... I need it before that, because it seems to the user as has the app crashed, while waiting for the new view to show. Do you have any suggestions? I thought of something like this:

in app delegate:
two methods - one to start and one to stop the animation of the indicator
in applicationDidFinishLaunching is the indicator object initiated.
The appDelegate is a tabBarViewControllerDelegate and impl the method: – tabBarController:didSelectViewController: to detect when one of the tabs are clicked and the startAnimation method is called.

in the viewControllers:
in viewDidLoad method is the stopAnimation method called.

thanks in advance!
MACloop
 
...but the problem is that the indicator only shows upp when the "new view" is displayed... I need it before that, because it seems to the user as has the app crashed, while waiting for the new view to show. Do you have any suggestions?
Just remember that if you are downloading on the main thread you will block out any updates to the UI. I would suggest this approach: 1) start the activity indicator animation, 2) have the data download asynchronously, 3) when done, stop the activity indicator on the main thread.
 
Just remember that if you are downloading on the main thread you will block out any updates to the UI. I would suggest this approach: 1) start the activity indicator animation, 2) have the data download asynchronously, 3) when done, stop the activity indicator on the main thread.

Thanks alot for your answer. How do I start the download asynchronously? I do init the viewController with a tabBar and in the ViewDidLoad method do I create the actual view. At the end of viewDidLoad do I call my method to parse the data from a XML file. This parsing is tha problem. Where would you suggest me to call this method?
 
dejo's solution is the standard for getting large amounts of data. That would have been my recommendation.
 
just one small thing...

I have tried NSThread out and it seem to work very nice. I just want to assure that this is the way to use it:

In my viewDidLoad method:
Code:
[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];

In myMethod:
Code:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
doing the data download
[pool release];

If updating the app with new data again I do just the same once again (event from update button)

MACloop
 
If you're just downloading data over the internet and you need it to be asynchronous, use NSURLConnection. Don't use the synchronous methods and put it in a thread. That's not good. An NSURLConnection object can be cancelled properly, unlike the synchronous thread method. Plus if you don't know how to setup a thread, you shouldn't be using threads.
 
If you're just downloading data over the internet and you need it to be asynchronous, use NSURLConnection. Don't use the synchronous methods and put it in a thread. That's not good. An NSURLConnection object can be cancelled properly, unlike the synchronous thread method. Plus if you don't know how to setup a thread, you shouldn't be using threads.

Hi again,
thanks for your comment! I will loook at the NSURLConnection but am curious about why my solution is "not good". I read data from a XML file once when the app starts and then again if the user likes to update it. It is not somthing going on all the time in the background. I know how to use threads from other program languages like C and Java and thought it could be fine to use in a situation like this. The only reason it so assure that the data are downloaded at the moment when I start to use them.

Is there roubustness issues with using threads like I do or is it bad seen from a memory point of view? My app is working fine with this soultuion but thats no guarentee for roubustness, I know....

Also - it does not matter in my app if the user has to wait for some secounds, meaning making the app wait until the thread is done is ok. I just want to be sure there are no "problems" regaring roboustness using a thread like I do...

Once again thanks for your comments and help - it is really helpful!
MACloop
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.