iphone - NSURLConnection and grand central dispatch -


is advisable wrap nsurlconnection in gcd style blocks , run on low_priority queue?

i need ensure connections not happening on main thread , connections need asynchronous. need several simultaneous requests go @ once.

if go gcd route, i'm not sure thread nsurlconnectiondelegate methods invoked on.

nsurlconnection relies on delegates once connection complete, whatever wrapper class handles need invoke caller. i'm not how deal of various callbacks invoked when connection work starts up/finishes:

- (void)connection:(nsurlconnection *)aconnection didreceiveresponse:(nsurlresponse *)response; - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)incrementaldata; - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error; - (void)connectiondidfinishloading:(nsurlconnection *)connection; 

should call synchronous versions wrapped in gcd blocks? , if want cancel call, use 'dispatch_suspend'?

dispatch_async(queue,^{       nsstring* result = [self mysynchronoushttp:someurltoinvoke];       });  // if need cancel dispatch_suspend(queue); 

i recommend see wwdc sessions network application in iphone os.

  • wwdc 2010 session 207 - network apps iphone os, part 1
  • wwdc 2010 session 208 - network apps iphone os, part 2

the lecturer said

"threads evil™"

for network programming , recommended use asynchronous network programming runloop. use background thread (grand central dispatch concurrent queue) thread-safe data processing, not network programming.

by way, blocks , grand central dispatch sessions worth see.

  • wwdc 2010 session 206 - introducing blocks , grand central dispatch on iphone
  • wwdc 2010 session 211 - simplifying iphone app development grand central dispatch

i wrote wrapper class asynchronous nsurlconnection.


asyncurlconnection.h

#import <foundation/foundation.h>  typedef void (^completeblock_t)(nsdata *data); typedef void (^errorblock_t)(nserror *error);  @interface asyncurlconnection : nsobject {     nsmutabledata *data_;     completeblock_t completeblock_;     errorblock_t errorblock_; }  + (id)request:(nsstring *)requesturl completeblock:(completeblock_t)completeblock errorblock:(errorblock_t)errorblock; - (id)initwithrequest:(nsstring *)requesturl completeblock:(completeblock_t)completeblock errorblock:(errorblock_t)errorblock; @end 

asyncurlconnection.m

#import "asyncurlconnection.h"  @implementation asyncurlconnection  + (id)request:(nsstring *)requesturl completeblock:(completeblock_t)completeblock errorblock:(errorblock_t)errorblock {     return [[[self alloc] initwithrequest:requesturl         completeblock:completeblock errorblock:errorblock] autorelease]; }  - (id)initwithrequest:(nsstring *)requesturl completeblock:(completeblock_t)completeblock errorblock:(errorblock_t)errorblock {      if ((self=[super init])) {         data_ = [[nsmutabledata alloc] init];          completeblock_ = [completeblock copy];         errorblock_ = [errorblock copy];          nsurl *url = [nsurl urlwithstring:requesturl];         nsurlrequest *request = [nsurlrequest requestwithurl:url];         [nsurlconnection connectionwithrequest:request delegate:self];     }      return self; }  - (void)dealloc {     [data_ release];      [completeblock_ release];     [errorblock_ release];     [super dealloc]; }  - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response {     [data_ setlength:0]; }  - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data {     [data_ appenddata:data]; }  - (void)connectiondidfinishloading:(nsurlconnection *)connection {     completeblock_(data_); }  - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error {     errorblock_(error); }  @end 

how use asyncurlconnection class.

/*  * asyncurlconnection -request:completeblock:errorblock: have called  * main thread because required use asynchronous api runloop.  */  [asyncurlconnection request:url completeblock:^(nsdata *data) {      /* success! */      dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{          /* process downloaded data in concurrent queue */          dispatch_async(dispatch_get_main_queue(), ^{              /* update ui on main thread */          });     });  } errorblock:^(nserror *error) {      /* error! */  }]; 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -