Welcome to the Real Reactive World

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
April 03, 2017

Welcome to the Real Reactive World

Welcome to the Real Reactive World
Realm World Tour, Seoul, 2017
Sangjoon Moon

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

April 03, 2017
Tweet

Transcript

  1. Welcome to the Real Reactive World Realm World Tour, Seoul,

    2017 Sangjoon Moon
  2. Erik Meijer GOTO 2015 One Hacker Way https://www.youtube.com/watch?v=FvMuPtuvP5w

  3. When you want to stay up to date, push not

    pull Erik Meijer
  4. Pull Driven • Too Fast • The Wrong Moment •

    Slow https://dribbble.com/shots/2141820-Latest-News-Pull-to-Refresh
  5. Push Driven https://dribbble.com/shots/1473664-Android-Wear-Clock-App-GIF

  6. Push Driven Real-time Data Sync • Realm Mobile Platform •

    Google Firebase Real-time Database
  7. How It Works(Clients) • Authentication • Configuration • Data Sync

  8. Authentication(Realm) private var user: SyncUser? func logIn(completion: ((Void)->Void)? = nil)

    { // You can use SyncUser.current to check logged in user guard user == nil else { completion?() return } let credentials = SyncCredentials.usernamePassword(username: username, password: password) SyncUser.logIn(with: credentials, server: authServerURL) { (user, error) in if let user = user { self.user = user DispatchQueue.main.async { completion?() } } else if let error = error { // handling the error properly } } } Tracking Santa With Realm https://realm.io/news/track-santa-with-realm-swift-database-platform-part-3/
  9. Configuration(Realm) // opening a standalone Realm let realm = try!

    Realm()
  10. Configuration(Realm) // Create the configuration for the synced Realm let

    syncServerURL = URL(string: "realm://localhost:9080/~/ userRealm")! let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: syncServerURL)) // Open the remote Realm let realm = try! Realm(configuration: config)
  11. Configuration Wrapper(Realm) // SantaRealmManager.swift private func realm(for user: SyncUser?, at

    syncServerURL: URL) -> Realm? { guard let user = user else { return nil } let syncConfig = SyncConfiguration(user: user, realmURL: syncServerURL) let config = Realm.Configuration(syncConfiguration: syncConfig) guard let realm = try? Realm(configuration: config) else { fatalError("Could not load Realm") } return realm } Tracking Santa With Realm https://realm.io/news/track-santa-with-realm-swift-database-platform-part-4/
  12. Configuration Wrapper(Realm) private let realmManager = SantaRealmManager() realmManager.logIn { if

    let realm = self.realmManager.santaRealm() { let santas = realm.objects(Santa.self) … } } // SantaRealmManager.swift func santaRealm() -> Realm? { return realm(for: user, at: santaRealmURL) } Tracking Santa With Realm https://realm.io/news/track-santa-with-realm-swift-database-platform-part-4/
  13. Data Sync(Realm) • Notification(Realm, Collection, Object) • KVO

  14. Data Sync(Realm) // Object Notifications var token : NotificationToken? //

    change is the type of enum. token = stepCounter.addNotificationBlock { change in switch change { case .change(let properties): // PropertyChange is the type of struct for property in properties { if property.name == "steps" && property.newValue as! Int > 1000 { print("Congratulations, you've exceeded 1000 steps.") token = nil } } case .error(let error): print("An error occurred: \(error)") case .deleted: print("The object was deleted.") } }
  15. Configuration(Firebase) // application:didFinishLaunchingWithOptions: // Initialize Firebase FIRApp.configure() // Firebase Console

    // Rules to restrict to authenticated users for the path `messages` { "rules": { "messages": { ".read": "auth != null", ".write": "auth != null" } } }
  16. By default, read and write access to your database is

    restricted so only authenticated users can read or write data. Firebase Docs https://firebase.google.com/docs/database/ios/read-and-write
  17. Authentication(Firebase) // google account sign in func sign(_ signIn: GIDSignIn!,

    didSignInFor user: GIDGoogleUser!, withError error: Error?) { if let error = error { print("Error \(error)") return } guard let authentication = user.authentication else { return } let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken) FIRAuth.auth()?.signIn(with: credential) { (user, error) in if let error = error { print("Error \(error)") return } } }
  18. Data Sync(Firebase) var ref: FIRDatabaseReference! fileprivate var _refHandle: FIRDatabaseHandle? func

    updateUI() { ref = FIRDatabase.database().reference() // Listen for new messages in the Firebase database _refHandle = self.ref.child("condition").observe(.value, with: { [weak self] (snapshot) -> Void in guard let strongSelf = self else { return } strongSelf.conditionLabel.text = snapshot.value as? String }) } deinit { // If an observer isn't properly removed, it continues to sync data to local memory if let refHandle = _refHandle { ref.child("condition").removeObserver(withHandle: refHandle) } } Firebase Database Demo https://www.youtube.com/watch?v=joVi3thZOqc
  19. ROS / Firebase • Data Structure(Realm Model / JSON) •

    Hosting Types • Server Event Handling • Dashboard • Offline Capabilities
  20. Summary • Push Driven • Auth, Config, Data Sync •

    Build Better Apps Faster
  21. Questions? Sangjoon Moon iceblue.dev@gmail.com dakeshi@github