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

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi all..


In my app,i want to show a wait screen , and download the some stuffs remove the wait screen and populate the data on a new View....

So what i did is..
Code:
init window
[window addSubView:waitScreen];
[window makeKeyAndVisible];

[self doDownlaod];

[COLOR="Red"]if(success)
[waitScreen removeFromSuperView];
[window addSubView:mainView];
[/COLOR]

But its not showing teh waitScreen..

I commented those in RED FONT found that the waitSCreen is coming visible only after the download....

help !!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I bet your download is synchronous and blocking the UI from updating. Make it asynchronous.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
If your [self doDownlaod]; (which is mis-spelt btw) is blocking then you will, most likely, be blocking the runloop. As the run loop is blocked no UI activities like displaying a new view will happen until the run loop unblocks.

The solution is simple: use the detachNewThreadSelector:toTarget:withObject: method of NSThread to start the doDownload method in a new thread and have doDownload either remove the waitScreen or call a method to remove it when it's done. You might find that you need to remove the waitScreen on the main thread (I had some stability issues when I was performing UI actions using a non-main thread, but this might have been my fault). You can do this via the performSelectorOnMainThread:withObject:waitUntilDone: method of NSObject.

So the code would look something like. How you pass success around etc is up to you. This code is untested: it's just intended to give you the general idea.

Code:
-(void) downloadComplete
{
if(success)
[waitScreen removeFromSuperView];
[window addSubView:mainView];
}

-(void) doDownload
{
// Do your download/parsing

[self performSelectorOnMainThread:@selector(downloadComplete) withObject:self waitUntilDone:NO];
}

-(void) startDownload
{
init window
[window addSubView:waitScreen];
[window makeKeyAndVisible];

[NSThread detachNewThreadSelector:@selector(doDownload) toTarget:self withObject:nil];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.