title: AFNetworking Asynchronous Data Fetching
body: <p>I'm using the AFNetworking library to pull a JSON feed from a server to populate a <code>UIPickerView</code>, but I'm having a little trouble wrapping my head around the asynchronous way of doing things. The <code>@property</code> <code>classChoices</code> is an <code>NSArray</code> that's being used to populate the <code>UIPickerView</code>, so that the web call is only performed once. However, since the block isn't finished by the time the instance variable is returned, the getter returns nil, and it eventually causes my program to crash later on. Any help in fixing this would be greatly appreciated. Let me know if you need any additional information.</p>
  
  <p><strong>PickerViewController.m classChoices Getter</strong></p>
  
  <pre><code>- (NSArray *)classChoices {
      if (!_classChoices) {
          // self.brain here refers to code for the SignUpPickerBrain below
          [self.brain classChoicesForSignUpWithBlock:^(NSArray *classChoices) {
              _classChoices = classChoices;
          }];
      }
      return _classChoices;
  }
  </code></pre>
  
  <p><strong>SignUpPickerBrain.m</strong></p>
  
  <pre><code>- (NSArray *)classChoicesForSignUpWithBlock:(void (^)(NSArray *classChoices))block {
      [[UloopAPIClient sharedClient] getPath:@"mobClass.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseJSON) {
          NSLog(responseJSON);
          if (block) {
              block(responseJSON);
          }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
  
          if (block) {
              block(nil);
          }
      }];
  }
  </code></pre>