Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase

 Firebase

Presented at Cocoaheads Belgium, 16/5/2017

Apokrupto

May 05, 2017
Tweet

More Decks by Apokrupto

Other Decks in Programming

Transcript

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

    View Slide

  2. FIREBASE
    ▸ Live coding
    THE PLAN - WORKING BACKWARDS

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. COUPLING

    View Slide

  7. FIREBASE
    ARCHITECTURE
    What I learned from Parse

    View Slide

  8. FIREBASE
    ARCHITECTURE
    PARSE
    What I learned from Parse

    View Slide

  9. FIREBASE
    ARCHITECTURE
    PARSE
    PARSE
    What I learned from Parse

    View Slide

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

    View Slide

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

    View Slide

  12. FIREBASE
    ARCHITECTURE
    The avoidance of hard coded dependencies

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  21. STORE

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  31. FIREBASE
    ARCHITECTURE
    PERSISTENCE

    View Slide

  32. FIREBASE
    LIVE CODING
    SINK SINK
    SOURCE
    STORE
    SINK

    View Slide

  33. FIREBASE
    LIVE CODING
    SINK SINK
    SOURCE
    STORE
    SINK
    FIREBASE

    View Slide

  34. FIREBASE

    View Slide

  35. FIREBASE
    SETUP

    View Slide

  36. FIREBASE

    View Slide

  37. FIREBASE

    View Slide

  38. FIREBASE
    ▸ Obj-C
    SDK

    View Slide

  39. FIREBASE
    ▸ Obj-C
    ▸ Cocoapods
    SDK

    View Slide

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

    View Slide

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

    View Slide

  42. LOGIN

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  47. FIREBASE
    ▸ Button drop shadow is not
    configurable
    LOGIN

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  51. FIREBASE
    ▸ Providers present a modal Safari
    for web login
    ▸ Email & password is handled
    differently
    LOGIN

    View Slide

  52. FIREBASE
    Podfile:
    pod 'Firebase/Core'
    pod 'Firebase/Auth'
    pod 'FirebaseUI'
    Source:
    import FirebaseAuthUI
    import FirebaseFacebookAuthUI
    import FirebaseGoogleAuthUI
    import FirebaseTwitterAuthUI
    LOGIN

    View Slide

  53. FIREBASE
    guard let authUI = FUIAuth.defaultAuthUI() else {
    return
    }
    authUI.isSignInWithEmailHidden = false
    authUI.providers = [
    FUIGoogleAuth(),
    FUITwitterAuth()
    ]
    present(authUI.authViewController(),
    animated: true,
    completion: nil)
    LOGIN

    View Slide

  54. FIREBASE
    ▸ Login status can change
    LOGIN

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  59. FIREBASE
    func addStateDidChangeListener(_ :
    (FIRAuth, FIRUser?) -> Void) ->
    FIRAuthStateDidChangeListenerHandle
    LOGIN

    View Slide

  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

    View Slide

  61. DATABASE

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  68. FIREBASE

    View Slide

  69. DATABASE
    REFERENCE

    View Slide

  70. DATABASE
    REFERENCE

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  80. FIREBASE
    ▸ Small example - so MVC
    LIVE CODING

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  85. FIREBASE
    LIVE CODING
    SINK SINK
    SOURCE
    STORE
    SINK

    View Slide

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

    View Slide

  87. FIREBASE
    LIVE CODING
    SOURCE
    STORE
    SINK
    FIREBASE
    UI

    View Slide

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

    View Slide