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 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. FIREBASE ▸ A good/simple architecture ▸ But can be improved

    ▸ Data may be created at one location and consumed elsewhere. STORE
  12. 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
  13. 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
  14. 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
  15. FIREBASE protocol Source { var onData: ((Int) -> Void) {

    get set } } protocol Sink { var writeData: ((Int) -> Void) { get } } protocol Store { init(source: Source, sinks: [Sink]) } STORE
  16. 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
  17. FIREBASE ▸ Obj-C ▸ Cocoapods ▸ Unofficial carthage support @

    https://github.com/ soheilbm/Firebase ▸ Java ▸ JavaScript SDK
  18. FIREBASE ▸ Firebase comes with a default authentication view controller

    ▸ Can be configured for multiple authentication providers LOGIN
  19. 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
  20. 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
  21. FIREBASE ▸ Button drop shadow is not configurable ▸ Buttons

    do not use dynamic type ▸ You need to register your app with Facebook LOGIN
  22. 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
  23. FIREBASE ▸ Providers present a modal Safari for web login

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

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

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

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

    but they are automatically refreshed ▸ The logged in user is `FIRAuth.auth()?.currentUser` LOGIN
  28. 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
  29. 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
  30. 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
  31. FIREBASE ▸ FIRDatabase ▸ Entry point for accessing the data

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

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

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

    ▸ Not the database data itself ▸ Controls online/offline mode ▸ Persistence, persistence cache size ▸ Callback queue DATABASE
  35. 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
  36. FIREBASE ▸ Represents a node in the database ▸ Read,

    write & remove data to/from the node DATABASE REFERENCE
  37. 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
  38. 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
  39. 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
  40. FIREBASE ▸ Represents a node in the database ▸ Read

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

    data by async observations ▸ Large number of observing options, 8 different APIs DATABASE REFERENCE
  42. 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
  43. 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
  44. FIREBASE ▸ Small example - so MVC ▸ Using Store-based

    architecture ▸ Generate data to write out to Firebase LIVE CODING
  45. 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
  46. 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