title: xcode NSURLConnection NSMutableData appendData only works for certain types of content
body: <p>I have created a class for fetching server-side data in my iOS-App, based on NSURLConnection, forwarding the fetched data to a delegate for processing.</p>
  
  <p>When downloading text/json this works perfectly.</p>
  
  <p>I am now expanding to fetch some png-images and can't get the data through.</p>
  
  <p>I have located that it breaks in the didReceiveData-call, where [receivedData appendData:data] do not do anything for the received image-data.</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
      // Append the new data to receivedData.
      [receivedData appendData:data];
  }
  </code></pre>
  
  <p>I get data into this method (NSData >0 bytes), but calling [receivedData appendData:data] do not change the size of receivedData (Still 0 bytes).</p>
  
  <p>As this works perfectly with text and not with image-data I believe it has something to do with characterset or encoding to do, but cannot find anything on this.</p>
  
  <p>Any help is appreciated.</p>
  
  <hr>
  
  <p><strong>Update</strong> with requested code-samples:</p>
  
  <p>In the method that makes the call:</p>
  
  <pre><code>NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  if (theConnection) {
      // Create the NSMutableData to hold the received data.
      receivedData = [NSMutableData data];
      // Also tried with no change in behaviour
      // receivedData = [[NSMutableData alloc] initWithLength:0];
  } else {
      // Inform the user that the connection failed.
  }
  </code></pre>
  
  <p>My <code>didReceiveResponse</code>:</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  {
      [receivedData setLength:0];
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Another UPDATE</strong></p>
  
  <p>Turns out the problem bois down to concurrency as I fire three instances of the class simultaniously to fetch three files asynchroniously. The problem is that the first releases <code>receivedData</code> when finishing ruining the response from the two other instances.</p>
  
  <p>How do I make sure that each instance of the class has their own <code>receivedData</code> to play with?</p>
  
  <hr>
  
  <p><strong>Final Notes</strong>
  Using a dictionary of MutableArrays for the retreived data solves the concurrency-problem.</p>
  
  <p>It also seems like defining the MutableArray as a private property of the class creates one array for each instance.</p>
  
  <pre><code>@interface MyClass(){
      @private NSMutableData * receivedData;
  }
  </code></pre>