title: How to save data out of an iOS completion block
body: <p>I'm basically implementing a fancier NSURLConnection class that downloads data from a server parses it into a dictionary, and returns an NSDictionary of the data. I'm trying add a completion block option (in addition to a delegate option), but it crashes anytime I try to store that data in another class. </p>
  
  <pre><code>        [dataFetcher_ fetchDataWithURL:testURL completionHandler:^(NSDictionary *data, NSInteger error) {        
              contentDictionary_ = data;            
      }];
  </code></pre>
  
  <p>I can NSLog that data just fine, and basically do whatever I want with it, but as soon as I try to save it into another variable it crashes with a really obscure message. </p>
  
  <p>EDIT: the crash message is EXC_BAD_ACCESS, but the stack trace is 0x00000000 error: address doesn't contain a section that points to a section in a object file.</p>
  
  <p>I'm calling this function in the init method of a singleton. It DOES let me save the data if I set this in the completion block.   </p>
  
  <pre><code>[SingletonClass sharedInstance].contentDictionary = data
  </code></pre>
  
  <p>But then the app gets stuck forever because sharedInstance hasn't returned yet, so the singleton object is still nil, so sharedInstance in the completion block calls init again, over and over. </p>
  
  <p>EDIT 2: The singleton code looks like this: </p>
  
  <pre><code>+ (SingletonClass*)sharedInstance {
     static SingletonClass *instance;
     if (!instance) {
         instance = [[SingletonClass alloc] init];
     }
     return instance;
  }
  
  
  - (id)init {
      self = [super init];
      if (self) {
          dataFetcher_ = [[DataFetcher alloc] init];
          NSString *testURL = @"..."  
          [dataFetcher_ fetchDataWithURL:testURL completionHandler:^(NSDictionary *data, NSInteger error) {        
              [SingletonClass sharedInstance].contentDictionary = data;
  
          }];
  
      }
      return self;
  }
  </code></pre>
  
  <p>Like I said, this works fine but repeats the initialize code over and over until the app crashes. This only happens the first time I run the app on a device, because I cache the data returned and it doesn't crash once I have the data cached. I would like to be able to just say self.contentDictionary = data, but that crashes. </p>