Multiple async NSURLConnections


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*-------------------------------------------------------------
  2. * DECLORATION
  3. *------------------------------------------------------------*/
  4.  
  5. NSMutableDictionary *receivedData;
  6.  
  7. /*-------------------------------------------------------------
  8. * IMPLEMENTATION
  9. *------------------------------------------------------------*/
  10.  
  11. - (void)startAsyncLoad:(NSURL*)url tag:(NSString*)tag {
  12. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  13. CustomURLConnection *connection = [[CustomURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];
  14.  
  15. if (connection) {
  16. [receivedData setObject:[[NSMutableData data] retain] forKey:connection.tag];
  17. }
  18. }
  19.  
  20. - (NSMutableData*)dataForConnection:(CustomURLConnection*)connection {
  21. NSMutableData *data = [receivedData objectForKey:connection.tag];
  22. return data;
  23. }
  24.  
  25. - (void)load {
  26. receivedData = [[NSMutableDictionary alloc] init];
  27.  
  28. NSURL *url1 = [NSURL URLWithString:@"http://blog.emmerinc.be"];
  29. NSURL *url2 = [NSURL URLWithString:@"http://www.emmerinc.be"];
  30. NSURL *url3 = [NSURL URLWithString:@"http://twitter.com/emmerinc"];
  31.  
  32. [self startAsyncLoad:url1 tag:@"tag1"];
  33. [self startAsyncLoad:url2 tag:@"tag2"];
  34. [self startAsyncLoad:url3 tag:@"tag3"];
  35. }
  36.  
  37. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  38. NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
  39. [dataForConnection setLength:0];
  40. }
  41.  
  42. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  43. NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
  44. [dataForConnection appendData:data];
  45. }
  46.  
  47. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  48. NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
  49. [connection release];
  50.  
  51. // Do something with the dataForConnection.
  52. }
  53.  
  54. /*-------------------------------------------------------------
  55. * CUSTOM NSURLCONNECTION CLASS
  56. *------------------------------------------------------------*/
  57.  
  58. @interface CustomURLConnection : NSURLConnection {
  59. NSString *tag;
  60. }
  61.  
  62. @property (nonatomic, retain) NSString *tag;
  63.  
  64. - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString*)tag;
  65.  
  66. @end
  67.  
  68. /*-------------------------------------------------------------
  69. * IMPLEMENTATION
  70. *------------------------------------------------------------*/
  71.  
  72. @implementation CustomURLConnection
  73.  
  74. @synthesize tag;
  75.  
  76. - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString*)tag {
  77. self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately];
  78.  
  79. if (self) {
  80. self.tag = tag;
  81. }
  82. return self;
  83. }
  84.  
  85. - (void)dealloc {
  86. [tag release];
  87. [super dealloc];
  88. }
  89.  
  90. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.