Firebase

 Firebase

Presented at Cocoaheads Belgium, 16/5/2017

A84ff6526157fd1d165bfde743367382?s=128

Apokrupto

May 05, 2017
Tweet

Transcript

  1. FIREBASE COCOAHEADS BELGIUM - MAY 2017 WARREN GAVIN (@APOKRUPTO)

  2. FIREBASE ▸ Live coding THE PLAN - WORKING BACKWARDS

  3. FIREBASE ▸ Live coding ▸ Configuration THE PLAN - WORKING

    BACKWARDS
  4. FIREBASE ▸ Live coding ▸ Configuration ▸ Firebase THE PLAN

    - WORKING BACKWARDS
  5. FIREBASE ▸ Live coding ▸ Configuration ▸ Firebase ▸ Architecture

    THE PLAN - WORKING BACKWARDS
  6. COUPLING

  7. FIREBASE ARCHITECTURE What I learned from Parse

  8. FIREBASE ARCHITECTURE PARSE What I learned from Parse

  9. FIREBASE ARCHITECTURE PARSE PARSE What I learned from Parse

  10. FIREBASE ARCHITECTURE PARSE PARSE PARSE What I learned from Parse

  11. FIREBASE ARCHITECTURE PARSE PARSE PARSE PARSE What I learned from

    Parse
  12. FIREBASE ARCHITECTURE The avoidance of hard coded dependencies

  13. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. The avoidance of hard coded dependencies
  14. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE The avoidance of hard coded dependencies
  15. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE FIREBASE The avoidance of hard coded dependencies
  16. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE NETWORKING ALAMOFIRE FIREBASE The avoidance of hard coded dependencies
  17. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE NETWORKING ALAMOFIRE ANALYTICS FIREBASE The avoidance of hard coded dependencies
  18. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE NETWORKING ALAMOFIRE ANALYTICS FIREBASE The avoidance of hard coded dependencies
  19. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE NETWORKING STUB ANALYTICS FIREBASE The avoidance of hard coded dependencies
  20. FIREBASE ARCHITECTURE If you are adding 3rd party services, keep

    them isolated from your app as much as possible. And they can be easily removed for unit testing. PERSISTENCE NETWORKING STUB ANALYTICS STUB STUB The avoidance of hard coded dependencies
  21. STORE

  22. FIREBASE ▸ Store ▸ Lies between your model and persistence

    layers ▸ Converts between raw persistence data and your model, and vice versa ARCHITECTURE VIEW CONTROLLER MODEL PERSISTENCE
  23. FIREBASE ▸ MVC-RS ▸ Greg Lhotellier (@greg3z) ▸ https://medium.com/swift-programming/mvc- rs-8780e73e9ff4

    ▸ R - Router, similar to Coordinator or VIPER ▸ S- Store ▸ Can be used with MVVM ARCHITECTURE
  24. FIREBASE ▸ A good/simple architecture ▸ But can be improved

    STORE
  25. FIREBASE ▸ A good/simple architecture ▸ But can be improved

    ▸ Data may be created at one location and consumed elsewhere. STORE
  26. FIREBASE ▸ A good/simple architecture ▸ But can be improved

    ▸ Data may be created at one location and consumed elsewhere. ▸ From: network, BLE, user interaction, database ▸ To: database, UI STORE
  27. FIREBASE ▸ A good/simple architecture ▸ But can be improved

    ▸ Data may be created at one location and consumed elsewhere. ▸ Read-only source ▸ Write-only sink STORE
  28. FIREBASE ▸ A good/simple architecture ▸ But can be improved

    ▸ Data may be created at one location and consumed elsewhere. ▸ Read-only source ▸ Write-only sink - or sinks STORE
  29. FIREBASE protocol Source { var onData: ((Int) -> Void) {

    get set } } protocol Sink { var writeData: ((Int) -> Void) { get } } protocol Store { init(source: Source, sinks: [Sink]) } STORE
  30. FIREBASE struct MyStore: Store { private var source: Source private

    let sinks: [Sink] init(source: Source, sinks: [Sink]) { self.source = source self.sinks = sinks self.source.onData = { value in self.sinks.forEach { $0.writeData(value) } } } } STORE
  31. FIREBASE ARCHITECTURE PERSISTENCE

  32. FIREBASE LIVE CODING SINK SINK SOURCE STORE SINK

  33. FIREBASE LIVE CODING SINK SINK SOURCE STORE SINK FIREBASE

  34. FIREBASE

  35. FIREBASE SETUP

  36. FIREBASE

  37. FIREBASE

  38. FIREBASE ▸ Obj-C SDK

  39. FIREBASE ▸ Obj-C ▸ Cocoapods SDK

  40. FIREBASE ▸ Obj-C ▸ Cocoapods ▸ Unofficial carthage support @

    https://github.com/ soheilbm/Firebase SDK
  41. FIREBASE ▸ Obj-C ▸ Cocoapods ▸ Unofficial carthage support @

    https://github.com/ soheilbm/Firebase ▸ Java ▸ JavaScript SDK
  42. LOGIN

  43. FIREBASE ▸ Firebase comes with a default authentication view controller

    LOGIN
  44. FIREBASE ▸ Firebase comes with a default authentication view controller

    ▸ Can be configured for multiple authentication providers LOGIN
  45. FIREBASE ▸ Firebase comes with a default authentication view controller

    ▸ Can be configured for multiple authentication providers ▸ Customisable via UIAppearance and other tricks, but not directly LOGIN
  46. FIREBASE ▸ Firebase comes with a default authentication view controller

    ▸ Can be configured for multiple authentication providers ▸ Customisable via UIAppearance and other tricks, but not directly ▸ Text, colours and icons are pre- set LOGIN
  47. FIREBASE ▸ Button drop shadow is not configurable LOGIN

  48. FIREBASE ▸ Button drop shadow is not configurable ▸ Buttons

    do not use dynamic type LOGIN
  49. FIREBASE ▸ Button drop shadow is not configurable ▸ Buttons

    do not use dynamic type ▸ You need to register your app with Facebook LOGIN
  50. FIREBASE ▸ Button drop shadow is not configurable ▸ Buttons

    do not use dynamic type ▸ You need to register your app with Facebook ▸ You can make your own, but it’ll take time. LOGIN
  51. FIREBASE ▸ Providers present a modal Safari for web login

    ▸ Email & password is handled differently LOGIN
  52. FIREBASE Podfile: pod 'Firebase/Core' pod 'Firebase/Auth' pod 'FirebaseUI' Source: import

    FirebaseAuthUI import FirebaseFacebookAuthUI import FirebaseGoogleAuthUI import FirebaseTwitterAuthUI LOGIN
  53. FIREBASE guard let authUI = FUIAuth.defaultAuthUI() else { return }

    authUI.isSignInWithEmailHidden = false authUI.providers = [ FUIGoogleAuth(), FUITwitterAuth() ] present(authUI.authViewController(), animated: true, completion: nil) LOGIN
  54. FIREBASE ▸ Login status can change LOGIN

  55. FIREBASE ▸ Login status can change ▸ Tokens can expire,

    but they are automatically refreshed LOGIN
  56. FIREBASE ▸ Login status can change ▸ Tokens can expire,

    but they are automatically refreshed ▸ The logged in user is `FIRAuth.auth()?.currentUser` LOGIN
  57. FIREBASE ▸ Login status can change ▸ Tokens can expire,

    but they are automatically refreshed ▸ The logged in user is `FIRAuth.auth()?.currentUser` ▸ But don’t trust it, it’s cached. LOGIN
  58. FIREBASE ▸ Login status can change ▸ Tokens can expire,

    but they are automatically refreshed ▸ The logged in user is `FIRAuth.auth()?.currentUser` ▸ But don’t trust it, it’s cached. ▸ Better to observe authentication state LOGIN
  59. FIREBASE func addStateDidChangeListener(_ : (FIRAuth, FIRUser?) -> Void) -> FIRAuthStateDidChangeListenerHandle

    LOGIN
  60. FIREBASE ▸ addStateDidChangeListener ▸ In class FIRAuth ▸ Called back

    immediately. And then after every state change, including token refresh ▸ So mostly can be ignored. ▸ But it will notify you of any enforced logouts. LOGIN
  61. DATABASE

  62. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

    DATABASE
  63. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

    ▸ Not the database data itself DATABASE
  64. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

    ▸ Not the database data itself ▸ Controls online/offline mode DATABASE
  65. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

    ▸ Not the database data itself ▸ Controls online/offline mode ▸ Persistence, persistence cache size DATABASE
  66. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

    ▸ Not the database data itself ▸ Controls online/offline mode ▸ Persistence, persistence cache size ▸ Callback queue DATABASE
  67. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

    ▸ Not the database data itself ▸ Controls online/offline mode ▸ Persistence, persistence cache size ▸ Callback queue (main thread by default) DATABASE
  68. FIREBASE

  69. DATABASE REFERENCE

  70. DATABASE REFERENCE

  71. FIREBASE ▸ Represents a node in the database DATABASE REFERENCE

  72. FIREBASE ▸ Represents a node in the database ▸ Read,

    write & remove data to/from the node DATABASE REFERENCE
  73. FIREBASE ▸ Represents a node in the database ▸ Read,

    write & remove data to/from the node ▸ Provides a lot of APIs with completion handlers in the case of errors DATABASE REFERENCE
  74. FIREBASE ▸ Represents a node in the database ▸ Read,

    write & remove data to/from the node ▸ Provides a lot of APIs with completion handlers in the case of errors ▸ Can specify priority for nodes to provide custom ordering of children DATABASE REFERENCE
  75. FIREBASE let root = FIRDatabase.database().reference() // create /path/to/node let reference1

    = root.child(“path").child("to").child("node") // alternative let reference2 = root.child(“path/to/node”) // Writes /path/to/node = 42 reference2.setValue(42) reference2.removeValue() DATABASE REFERENCE
  76. FIREBASE ▸ Represents a node in the database ▸ Read

    data by async observations DATABASE REFERENCE
  77. FIREBASE ▸ Represents a node in the database ▸ Read

    data by async observations ▸ Large number of observing options, 8 different APIs DATABASE REFERENCE
  78. FIREBASE ▸ Represents a node in the database ▸ Read

    data by async observations ▸ Large number of observing options, 8 different APIs ▸ Observe once, or on modification, on new children, on removal, on move or on any change DATABASE REFERENCE
  79. FIREBASE ▸ Represents a node in the database ▸ Read

    data by async observations ▸ Large number of observing options, 8 different APIs ▸ Observe once, or on modification, on new children, on removal, on move or on any change. ▸ Limit returns to get progressive downloads DATABASE REFERENCE
  80. FIREBASE ▸ Small example - so MVC LIVE CODING

  81. FIREBASE ▸ Small example - so MVC ▸ Using Store-based

    architecture LIVE CODING
  82. FIREBASE ▸ Small example - so MVC ▸ Using Store-based

    architecture ▸ Generate data to write out to Firebase LIVE CODING
  83. FIREBASE ▸ Small example - so MVC ▸ Using Store-based

    architecture ▸ Generate data to write out to Firebase ▸ Observe updates in Firebase and display them LIVE CODING
  84. FIREBASE ▸ Small example - so MVC ▸ Using Store-based

    architecture ▸ Generate data to write out to Firebase ▸ Observe updates in Firebase and display them ▸ Requires login LIVE CODING
  85. FIREBASE LIVE CODING SINK SINK SOURCE STORE SINK

  86. FIREBASE LIVE CODING SINK SINK SOURCE STORE SINK GENERATOR FIREBASE

    UI CONSOLE
  87. FIREBASE LIVE CODING SOURCE STORE SINK FIREBASE UI

  88. OBLIGATORY “THANK YOU” SLIDE Hit me up! Twitter: @apokrupto Email:

    warren@apokrupto.com