Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I handled multiple NSUrlConnections using a NSMutableDictionary which keeps track of which instance of NSMutableData a particular NSURLConnection should save its result to.</p> <p>At the beginning of my class I define:</p> <pre><code>NSMutableDictionary *dataDictionary; </code></pre> <p>Then in my loadData method, I have:</p> <pre><code> // Create the request NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:currentCam] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30]; // create the connection with the request // and start loading the data NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { NSMutableData *receivedData = receivedData = [[NSMutableData alloc] init]; //keep track of this connection by adding it to subViewDictionary and dataDictionary with appropriate objects. [dataDictionary setObject:receivedData forKey:[theConnection description]]; } else { NSLog(@"ERROR DOWNLOADING WITH NSURLCONNECTION"); } [theConnection release]; </code></pre> <p>I use [theConnection description] as my key and an instance of MSMutableData as the object in my dictionary, so later I can look up which instance goes with a particular connection. If you fail to do this you can have issues with data corruption (multiple connections can all save their data to the same variable).</p> <p>Then, I define the following NSURlConnection delegate methods:</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data { //look up in dictionary to find out which recievedData instance to use. NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; // Append the new data to receivedData. [theReceivedData appendData:data]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { //Lookup in the dictionary to find the correct instance of recievedData to put image into. NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; [theReceivedData setLength:0]; [activityIndicator stopAnimating]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { // inform the user that there was an error NSLog(@"Connection failed! Error - localizedDescription:%@ NSURLErrorFailingURLStringErrorKey:%@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; [theReceivedData release]; //remove keys for this connection since it did not load. [dataDictionary removeObjectForKey:[connection description]]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { //look up correct instance of recievedData in teh dictionary for this connection NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; NSLog(@"Succeeded! Received %d bytes of data",[theReceivedData length]); //---do stuff with data here// [theReceivedData release]; } </code></pre> <p>There is a good tutorial on NSURlConnection <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html" rel="nofollow">here</a> for a single connection. My code is based on that with the addition of the NSMutableDictionary to keep track of each NSUrlConnection and each NSMutableData instance. </p> <p>I hope that makes sense and is helpful!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload