Advertisement
Guest User

Untitled

a guest
Jun 12th, 2012
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. long polling in objective-C
  2. - (void) longPoll {
  3. //create an autorelease pool for the thread
  4. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  5.  
  6. //compose the request
  7. NSError* error = nil;
  8. NSURLResponse* response = nil;
  9. NSURL* requestUrl = [NSURL URLWithString:@"http://www.mysite.com/pollUrl"];
  10. NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl];
  11.  
  12. //send the request (will block until a response comes back)
  13. NSData* responseData = [NSURLConnection sendSynchronousRequest:request
  14. returningResponse:&response error:&error];
  15.  
  16. //pass the response on to the handler (can also check for errors here, if you want)
  17. [self performSelectorOnMainThread:@selector(dataReceived:)
  18. withObject:responseData waitUntilDone:YES];
  19.  
  20. //clear the pool
  21. [pool drain];
  22.  
  23. //send the next poll request
  24. [self performSelectorInBackground:@selector(longPoll) withObject: nil];
  25. }
  26.  
  27. - (void) startPoll {
  28. //not covered in this example: stopping the poll or ensuring that only 1 poll is active at any given time
  29. [self performSelectorInBackground:@selector(longPoll) withObject: nil];
  30. }
  31.  
  32. - (void) dataReceived: (NSData*) theData {
  33. //process the response here
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement