title: How to return an object from a class that uses NSURLConnection and it's delegate classes?
body: <p>I'm in the process of trying to move code from a UITableViewController class to a "helper" class.</p>
  
  <p>The code utilizes NSURLConnection to grab and parse JSON and then populate an NSMutableArray. </p>
  
  <p>What I'd like to do is call a method in my helper class that returns a NSMutableArray. What I don't understand is how to return the array from the connectionDidFinishLoading delegate class of NSURLConnection (where the array is actually built) as though it was from the originally called method that started the connection. In other words, how does the method that calls NSURLConnection get control back so it can return a value from the whole operation?</p>
  
  <p>Here are the relevant methods from the helper class. How do I get the getMovies method to return the listOfMovies that is built in the connectionDidFinishLoading delegate class?</p>
  
  <pre><code>-(NSMutableArray)getMovies:(NSURL*)url {
  
  responseData = [[NSMutableData data] retain];       
  
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  //NSURLRequest* request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];
  
  connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  
  [responseData setLength:0];
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  [responseData appendData:data];
  }
  
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  //TODO error handling for connection
  }
  
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {      
  
  //---initialize the array--- 
  
  listOfMovies = [[NSMutableArray alloc] init];
  
  tmdbMovies = [[NSArray alloc] init];
  posters = [[NSArray alloc] init];
  thumbs = [[NSDictionary alloc] init];
  
  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  
  SBJsonParser *json = [[SBJsonParser new] autorelease];
  
  tmdbMovies = [json objectWithString:responseString];
  
  // loop through all the top level elements in JSON
  for (id movie in tmdbMovies) {
  
      // 0 - Name
      // 1 - Meta
      // 2 - Url
  
  
      if ((NSNull *)[movie objectForKey:@"name"] != [NSNull null]) {
  
          if (![[movie objectForKey:@"name"] isEqualToString:@""]) {
              name = [movie objectForKey:@"name"];
          }
  
      } 
  
      if ((NSNull *)[movie objectForKey:@"info"] != [NSNull null]) {
  
          if (![[movie objectForKey:@"info"] isEqualToString:@""]) {
              meta = [movie objectForKey:@"info"];
          }
  
      } 
  
      if ((NSNull *)[movie objectForKey:@"thumb"] != [NSNull null]) {
  
          if (![[movie objectForKey:@"thumb"] isEqualToString:@""]) {
              thumbUrl = [movie objectForKey:@"thumb"];
          }
  
      } 
  
      NSLog(@"Name: %@", name);
      NSLog(@"Info: %@", meta);
      NSLog(@"Thumb: %@", thumbUrl);
  
      NSMutableArray *movieData = [[NSMutableArray alloc] initWithObjects:name,meta,thumbUrl,nil];
  
      // add movieData array to listOfJMovies array
      [listOfMovies addObject:movieData];
  
  
      [movieData release];
  }
  
  
  //FIXME: Connection warning
  if (connection!=nil) { 
      [connection release]; 
  }
  
  [responseData release]; 
  [responseString release];
  
  
  
  }
  </code></pre>