views:

45

answers:

2

Hi !

I have implemented a subview which is supposed to load immediately when I click a button in the parent view. After loading the subview(which is basically holding an activityindicator), the program is supposed to process a method(which gets data from a server, so takes time) in it.

However, I am unable to do it.

What happens now is, when I click the button on the parent view, it processes the method first and only after that does the subview load on screen.

Why is this so? Is there any specific functions I could use to make my method load only after the view has loaded?

+1  A: 

Hi Susanth,

I have faced the same problem and i have used NSAutoreleasePool and solved this problem. I hope it will help you.

- (void)viewDidLoad {

   [self performSelectorInBackground:@selector(loadXml) withObject:nil];

 }

 -(void) loadXml{

        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

       NSString * path =@"http:www.YOUR_RSS_FEED.com";

       [self parseXMLFileAtURL:path];       //(Instead of ViewDidAppear)

      [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

       [pool drain];

   }

Thanks.

Pugal Devan
@Warrior, Check my updated Answer.
Pugal Devan
@Pugal, Now its more clear...
Warrior
A: 

A few different approaches:

If the subview has functionality as loading an image and doing more complicated stuff than just being a view, a good approach is to make it into a ViewController instead.

In the top ViewController use:

- (void) loadView { 
    [super loadView];
}

to set up things that need to be ready upon displaying the view.

The use:

-(void) viewDidLoad {

}

to do additional setup.

In your case it sounds as if you need to add the indicator in the loadView method, then start the retrieval of data in the viewDidLoad method.

If you are accessing web services etc. always do this on a different thread. (look into NSOperation for a good, simple way of achieving this).

RickiG