Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Overcome your fear of implementing offline mode in your apps

2,658 views

Published on

Way too many apps on the App Store totally break when you loose connectivity. Have a look at some study cases and hopefully by the end you will figure out that implementing offline mode in your app is not that difficult at all.

Published in: Mobile
  • Be the first to comment

Overcome your fear of implementing offline mode in your apps

  1. 1. Offline Mode Overcome your fear of implementing in your apps www.addofflinemode.com
  2. 2. Offline Mode Overcome your fear of implementing in your apps www.addofflinemode.com
  3. 3. Marin Todorov (me)
  4. 4. raywenderlich.com
  5. 5. Today you will See common offline mode fails and wins Draw a lot of good advice in the process Finally have a look at few tools
  6. 6. Connected apps web services
  7. 7. Internet in your pocket
  8. 8. Web services in the cloud Photo credit: Flickr, Torkild RetvedtPhoto credit: Flickr, Tim Bates
  9. 9. … Photo credit: Flickr, Torkild RetvedtPhoto credit: Flickr, Marcin Kargol
  10. 10. Oops (no connection)
  11. 11. Maybe?
  12. 12. Ehm?
  13. 13. offline gprs/edge
  14. 14. Awesome :)
  15. 15. Awesome :)
  16. 16. - offline - bad connectivity - online 3 modes
  17. 17. - offline 3 modes
  18. 18. - bad connectivity 3 modes CTTelephonyNetworkInfo currentRadioAccessTechnology CTRadioAccessTechnologyGPRS CTRadioAccessTechnologyEdge CTRadioAccessTechnologyLTE CTRadioAccessTechnologyDidChangeNotification
  19. 19. - online 3 modes current step steps top pre-fetch
  20. 20. - rely on what makes sense UI wise
  21. 21. How to handle offline mode
  22. 22. There is no universal solution
  23. 23. THE END
  24. 24. - asses the problem - evaluate the solution - code 3 steps in this order please!
  25. 25. Klout
  26. 26. Klout NSUserDefaults name score 4 last updates 2 last connections JSON
  27. 27. Klout Online? Start Fetch JSON UI Code Done Start Fetch JSON UI Code Done Local JSON Store locally YESNO
  28. 28. Simple solution for a simple problem First asses, then evaluate, then code
  29. 29. York cinemas
  30. 30. York cinemas
  31. 31. cinemas in Berlin - these change once a year ! film schedule changes every thursday ! static information - practically doesn’t change ! ticket reservations Photo credit: Flickr, Tobias Abel Photo credit: Flickr, InSapphoWeTrust
  32. 32. 12 cinema photos + their names + coordinates 25 film posters + film names 30 movie times for all cinemas ! 7KB sqlite file 2MB of photos ! = ! < 1 Facebook photo in size
  33. 33. 20.03 Thu Sat 27.03 Thu
  34. 34. Check this out York cinemas! This week? Start Read sqlite database UI Code Done Start Fetch 1 file UI Code Done YES NO Read sqlite database
  35. 35. The problem definition contained the solution Sqlite database is a single file You don’t need past data
  36. 36. How about ticket reservations?
  37. 37. You got to to some things online!
  38. 38. You can still do SOMETHING
  39. 39. A stock trading app price changes all the time ! displays historical data ! allows for real-time buying/selling
  40. 40. price changes all the time ! displays historical data ! allows for real-time buying/selling Past data does not change…
  41. 41. 14:40 14:50 15:00 14:30 14:40 14:50
  42. 42. 14:40 14:50 15:00 Time on plane
  43. 43. 14:50 15:00 Time on plane BUY 3 hrs
  44. 44. For long lists - use sqlite AAPL! 123456 11.6 10.5 123459 11.5 10.5 123460 12.0 11.0 123470 14.0 11.5 123490 14.0 10.5 Easy to sync to local database server database JSON local sqlite AAPL! ! ! ! 123470 14.0 11.5 123490 14.0 10.5 aapl keep ids from the server
  45. 45. How to fetch the delta data? 14:40 14:50 15:00 + tokenA + tokenB + tokenC + tokenA + tokenB
  46. 46. Facebook posts come in all the time ! posting in not time crucial ! chat with friends ! location check-in
  47. 47. 14:40 14:50 13:20 Time on plane
  48. 48. 14:40 14:50 13:20 Time on plane OMG! Great flight attendants on the flight to Lisbon!
  49. 49. Core Data = power + pain You need to recreate the relationships Migration strategy
  50. 50. What’s the foreseeable outcome of the create/edit operations? Data validation Cache the create operation data
  51. 51. 3 ways to store temporary objects ! 1. create the new objects, store them using NSCoding 2. create a separate store for pending objects 3. use existing store, but use ids < 0
  52. 52. How to change your app architecture
  53. 53. cloud Network layer REST JSON RPC JSON API XML? plist? Connectivity User Interface data storage NSCoding sqlite3 Core Data Cache file system Core Data file storage You probably have this setup
  54. 54. cloud Network layer REST JSON RPC JSON API XML? plist? Connectivity User Interface data storage NSCoding sqlite3 Core Data Cache file system Core Data file storage You want to have this setup
  55. 55. Prefer reading from local source, update local source from Internet
  56. 56. Power tools network, storage, UI …
  57. 57. sorry, iPhone only
  58. 58. CoreTelephony.framework CTTelephonyNetworkInfo -> currentRadioAccessTechnology ! CTTelephonyNetworkInfo -> subscriberCellularProvider.isoCountryCode !
  59. 59. NSURLSession NSURLSession CloudCode Cookie policy Auth policy Caching policy Storing files Timeouts etc. etc.
  60. 60. NSURLSessionConfiguration
  61. 61. Reachability https://github.com/tonymillion/Reachability ! arc, gdc, ios/osx, maximum wins reach  =  [Reachability  reachabilityWithHostname:@"www.addofflinemode.com"];   reach.unreachableBlock  =  ^(Reachability*reach)   {          NSLog(@"UNREACHABLE!");   };
  62. 62. JSONModel https://github.com/icanzilb/JSONModel ! model classes, validation, data conversion, etc.
  63. 63. JSONModel {“id”:1, “name”:”Marin”} NSNumber* id; NSString* name; NSManagedObject Model JSON
  64. 64. Core Data NSFetchedResultsController ! Core Data Editor Image: http://thermal-core.com/CoreDataEditor/
  65. 65. Core Data TBCoreDataStore! ! http://robots.thoughtbot.com/core-data Main Queue! context Background Queue! context read write merge Internet! JSON
  66. 66. FMDB https://github.com/ccgus/fmdb ! Objc sqlite made easy FMResultSet  *s  =  [db  executeQuery:@"SELECT  *  FROM  myTable"];   while  ([s  next])  {          //retrieve  values  for  each  record   }
  67. 67. NSCoding NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: myObject]; [encodedData writeToURL:localFileURL atomically:YES];
  68. 68. Cache simpler cache library - EGOCache https://github.com/enormego/EGOCache image caching - FastImageCache https://github.com/path/FastImageCache [[EGOCache globalCache] setObject:JSONResponse forKey:@"cachedJSON"]; [[EGOCache globalCache] setImage:myImage forKey:@"photo" 
 withTimeoutInterval:60*60];
  69. 69. Sequencer https://github.com/berzniz/Sequencer ! async flow control Get X Post Y Location Update UI [sequencer enqueueStep:^(id result, SequencerCompletion completion) { [self testSectionUnloggedGets: nil]; completion(nil); }]; ! [sequencer enqueueStep:^(id result, SequencerCompletion completion) { [self postToServer: ^{ completion(nil); }]; }];
  70. 70. What’s next?
  71. 71. Turn on plane mode and check how your favorite apps look like Check how your app looks like (eeek!) Get in touch, discuss, check out the power-tools
  72. 72. That’s all folks
  73. 73. Offline Mode Overcome your fear of implementing in your apps www.addofflinemode.com

×