window makeKeyAndVisible

Discussion in 'iOS Programming' started by sujithkrishnan, Aug 12, 2008.

  1. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #1
    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 !!
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I bet your download is synchronous and blocking the UI from updating. Make it asynchronous.
     
  3. sujithkrishnan thread starter macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #3
    @kainjow.....


    Its not actually a download...

    Its parsing a set of XMLs using NSXMLParser......
    What i can do with that????
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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];
    }
    
     

Share This Page