The Importance of   Reachability         Marc Weil      CTO, CloudMine     marc@cloudmine.me
Wireless connections aren’t perfect
Defensive Programming
Let’s look at a quick example
- (void)doLoad {    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];    NSHTT...
- (void)doLoad {    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];    [NSUR...
- (void)doLoad {    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];    [NSUR...
Queues are your best friend
InternetWhen everything is dandy...                                              HT                                       ...
InternetWhen everything is not so       dandy...                                             HTTP                         ...
How can I do this in code?      (apologies to Android devs)
Three components:1. NSOperationQueue2. NSOperation3. Reachability
NSOperationQueue *queue = [[NSOperationQueue alloc] init];[queue addOperationWithBlock:^{      NSURLRequest *req = [NSURLR...
NSOperationQueue *queue = [[NSOperationQueue alloc] init];[queue addOperationWithBlock:^{      NSURLRequest *req = [NSURLR...
Internet                                  HT                                    TP                             ue         ...
What about Reachability?
Everything is contained inSystemConfiguration.framework
But don’t use that directly... it’s terrible
https://github.com/tonymillion/Reachability          (love abstraction!)
// allocate a reachability objectReachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];reach.rea...
!                                    POW    Bringing out the big guns...https://github.com/AFNetworking/AFNetworking      ...
One more pet peeve.... are apps that don’t do this[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YE...
Resourceshttps://github.com/tonymillion/Reachabilityhttps://github.com/AFNetworking/AFNetworkinghttp://developer.apple.com...
Upcoming SlideShare
Loading in …5
×

Reachability in Mobile App Development

1,349 views

Published on

A 10 minute talk I gave at the API Strategy Conference in NYC on the importance of using reachability detection in conjunction with message queues when communicating with web services from mobile devices. Includes specific examples for iOS.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,349
On SlideShare
0
From Embeds
0
Number of Embeds
90
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Reachability in Mobile App Development

  1. 1. The Importance of Reachability Marc Weil CTO, CloudMine marc@cloudmine.me
  2. 2. Wireless connections aren’t perfect
  3. 3. Defensive Programming
  4. 4. Let’s look at a quick example
  5. 5. - (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; // responseBody contains the response body} Why is this naive?• Potentially blocks UI thread• No error handling• What happens if there’s no network connection?
  6. 6. - (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) { if (!err) { NSLog(@"Success!"); // body argument contains response body } else { NSLog(@"Error!"); } }];} Why is this still naive?• Potentially blocks UI thread• No error handling• What happens if there’s no network connection?
  7. 7. - (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) { if (!err) { NSLog(@"Success!"); // body argument contains response body } else { NSLog(@"Error!"); } }];} Why is this still naive?• Potentially blocks UI thread• No error handling• What happens if there’s no network connection?
  8. 8. Queues are your best friend
  9. 9. InternetWhen everything is dandy... HT TP ) HTTP eue qu HTTP e th Constant is HTTP is flow (th HTTP HTTP HTTP Request generator HTTP HTTP
  10. 10. InternetWhen everything is not so dandy... HTTP HTTP ) HTTP ue e qu HTTP e th Halted is HTTP is flow (th HTTP HTTP HTTP Request generator HTTP HTTP
  11. 11. How can I do this in code? (apologies to Android devs)
  12. 12. Three components:1. NSOperationQueue2. NSOperation3. Reachability
  13. 13. NSOperationQueue *queue = [[NSOperationQueue alloc] init];[queue addOperationWithBlock:^{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL];}];
  14. 14. NSOperationQueue *queue = [[NSOperationQueue alloc] init];[queue addOperationWithBlock:^{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL];}]; This is a convenience method that creates an NSBlockOperation under the hood.
  15. 15. Internet HT TP ue ue nQ io at er Op HTTP NS HTTP Constant HTTP flow NSOperation HTTP (NSOperations) HTTP HTTP Requestgenerator HTTP HTTP
  16. 16. What about Reachability?
  17. 17. Everything is contained inSystemConfiguration.framework
  18. 18. But don’t use that directly... it’s terrible
  19. 19. https://github.com/tonymillion/Reachability (love abstraction!)
  20. 20. // allocate a reachability objectReachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];reach.reachableOnWWAN = YES;// set the blocksreach.reachableBlock = ^(Reachability *reach){ queue.suspended = NO;};reach.unreachableBlock = ^(Reachability *reach){ queue.suspended = YES;};// start the notifier which will cause the reachability object to retain itself![reach startNotifier]; (example from readme on GitHub)
  21. 21. ! POW Bringing out the big guns...https://github.com/AFNetworking/AFNetworking (th an k sG ow all a!! <3! )
  22. 22. One more pet peeve.... are apps that don’t do this[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  23. 23. Resourceshttps://github.com/tonymillion/Reachabilityhttps://github.com/AFNetworking/AFNetworkinghttp://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.htmlContact Mee: marc@cloudmine.met: @marcweil

×