title: iphone tab bar app view life cycle question
body: <p>This is probably a stupid question with an easy answer. I'm new to iOS dev and this is killing me. </p>
  
  <p>I have a basic tab bar app. The app loads the initial tab upon launch and it is a tableview controller. The data for this tableview is supplied by a datacontroller where it creates an array from data fetched from the internet. When I first launch the app nothing is displayed in the tableview, it's empty. However, if I select a different tab and then go back to that tab the data is now there and displayed in the tableview as it should. How do I get the data to display the first time around? It's obviously there. I believe it may be something to do with where/when the data is loading that is causing the problem, any insight would be great.</p>
  
  <p>This sets the datacontroller in the app delegate:</p>
  
  <pre><code>// Create Locations data controller.
  LocationDataController *controller = [[LocationDataController alloc] init];
  self.locationDataController = controller;
  [controller release];
  locationsViewController.locationDataController = locationDataController;
  </code></pre>
  
  <p>Here's the datacontroller code:</p>
  
  <pre><code>@implementation LocationDataController
  @synthesize fetchConnection = mFetchConnection;
  @synthesize locations, tempLocations;
  
  
  
  -(id)initData {
      if (self = [super init]) {
          [self createData];
      }
      return self;
  }
  
  -(id)init {
      [self fetchData];
      return self;
  }
  
  
  -(unsigned)countOfList {
      return [locations count];
  }
  
  
  -(Location *)objectInListAtIndex:(unsigned)theIndex {
      return [locations objectAtIndex:theIndex];
  }
  
  
  -(void)dealloc {
      [locations release];
  
      if (mFetchConnection != nil) {
          [mFetchConnection cancel];
          [mFetchConnection release];
      }
  
      if (mFetchConnectionBuffer != nil)
          [mFetchConnectionBuffer release];
  
      [super dealloc];
  }
  
  
  #pragma mark -
  #pragma mark Fetching/Sending Data
  #pragma mark
  
  - (void)fetchData {
      if (mFetchConnection == nil) {
          if (mFetchConnectionBuffer != nil)
              [mFetchConnection release];
  
          mFetchConnectionBuffer = [[NSMutableData alloc] init];
  
          NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:kFetchURLPath]];
          self.fetchConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES] autorelease];
          [request release];
      }
  }
  
  
  
  
  #pragma mark -
  #pragma mark NSURLConnection Delegate
  #pragma mark
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)inData {
      if (connection == mFetchConnection) {
          [mFetchConnectionBuffer appendData:inData];
      }
  }
  
  
  
  
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)connectionError {
      if (connection == mFetchConnection) {
          self.fetchConnection = nil;
          [mFetchConnectionBuffer release];
          mFetchConnectionBuffer = nil;
      }
  }
  
  
  
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  
      tempLocations = [[NSMutableArray alloc] init];
  
      if (connection == mFetchConnection) {
          NSString *str = [[NSString alloc] initWithData:mFetchConnectionBuffer encoding:NSUTF8StringEncoding];
          SBJSON *parser = [[SBJSON alloc] init];
          id result = [parser objectWithString:str error:nil];
          if (result != nil &amp;&amp; [result isKindOfClass:[NSArray class]])
              [tempLocations setArray:result];
          [str release];
          [mFetchConnectionBuffer release];
          mFetchConnectionBuffer = nil;
          self.fetchConnection = nil;
  
          [self initData];
  
      }
  }
  
  
  -(void)createData {
  
      // Cycle through array to implement locations.
      Location *location;
      NSMutableArray *array = [[NSMutableArray alloc] init];
  
      // create arry of locations.
      for (int i=0; i&lt;[tempLocations count];i++) {
  
          location = [[Location alloc] init];
          location.name = [[tempLocations objectAtIndex:i] objectForKey:@"name"];
          location.address = [[tempLocations objectAtIndex:i] objectForKey:@"address"];
          location.city = [[tempLocations objectAtIndex:i] objectForKey:@"city"];
          location.state = [[tempLocations objectAtIndex:i] objectForKey:@"state"];
          location.zip = [[tempLocations objectAtIndex:i] objectForKey:@"zip"];
  
          [array addObject:location];
          [location release];
  
      }
  
      self.locations = array;
      [array release];
      [tempLocations release];
  
  }
  </code></pre>