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

Realm Centered Design

yohei sugigami
September 29, 2016

Realm Centered Design

RxSwift+RxRealm w/ RealmNotification

yohei sugigami

September 29, 2016
Tweet

More Decks by yohei sugigami

Other Decks in Technology

Transcript

  1. yohei SUGIGAMI
    Realm Centered Design
    RxSwift+RxRealm w/ RealmNotification
    Sep. 29, 2016 @ SanSan
    Realm meetup #19

    View Slide

  2. Yohei SUGIGAMI
    ID susieyy
    TwitterˍGithub & Qiita

    View Slide

  3. γΰτͰίίϩΦυϧ
    Launch iPhone3G
    iOS Developer since mid 2008

    View Slide

  4. γΰτͰίίϩΦυϧ
    WWDC 2014
    Swift Developer since Day1

    View Slide

  5. γΰτͰίίϩΦυϧ
    Working in a Wantedly

    View Slide

  6. Our issues
    in iOS development

    View Slide

  7. Our issues in iOS development
    Complex

    View Slide

  8. Our issues in iOS development
    State and data
    management
    Complex

    View Slide

  9. Our issues in iOS development
    Asynchronous
    processing
    Complex

    View Slide

  10. Our issues in iOS development
    Massive delegate
    pattern
    Complex
    https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
    Apple MVC Apple MVC

    View Slide

  11. Reactive program paradigm
    Observable
    Asynchronous data streams
    Functional program paradigm
    No side effects function
    Immutable structure
    Higher-order functions
    Lambda expression, monads
    Asynchronous friendly
    Unidirectional Data Flow
    Single source of truth
    The approach avoids complicated to simplify
    Redux
    RxSwift
    Reactive
    Cocoa
    simplify

    View Slide

  12. Hoping between RxSwift and ReSwift
    Redux
    Rx ???
    w/ MVVM
    (RxSwift) (ReSwift)
    Unidirectional 

    Data Flow

    View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. Approaching based on Rx with Realm
    Redux
    Rx Realm
    +
    w/ MVVM
    (RxSwift) (ReSwift)
    Unidirectional 

    Data Flow
    Unidirectional 

    Data Flow
    (RxRealm)

    View Slide

  19. Realm Centered Design
    View
    ViewModel
    Model
    Rx
    ( Local state )
    Store
    ( App state )
    Rx w/ MVVM

    View Slide

  20. Unidirectional Data Flow
    Transaction
    ( atomic )
    Notification
    Subscribe
    Realm Centered Design
    https://realm.io/news/unidirectional-data-flow-in-swift/

    View Slide

  21. Launch iPhone3G
    iOS Developer since mid 2008
    Key technologies
    Realm Notification
    RxRealm
    RxSwift w/ MVVM

    View Slide

  22. Launch iPhone3G
    iOS Developer since mid 2008
    Realm Notification
    Notifications section
    https://realm.io/docs/swift/latest/#notifications
    Collection Notifications, since ver 0.98
    https://realm.io/news/realm-objc-swift-0.98.0/
    Fine-grained change notifications, since ver 0.99
    https://realm.io/news/realm-objc-swift-0.99.0/

    View Slide

  23. Launch iPhone3G
    iOS Developer since mid 2008
    Do you know the Realm Notification?

    View Slide

  24. Launch iPhone3G
    iOS Developer since mid 2008
    You can be notified whenever a Realm,
    Results, List or LinkingObjects is updated by
    calling the addNotificationBlock method.
    a.k.a. Observe
    What Realm Notification

    View Slide

  25. Launch iPhone3G
    iOS Developer since mid 2008
    Realm Notifications
    Collection Notifications
    Fine-grained change
    Points

    View Slide

  26. Launch iPhone3G
    iOS Developer since mid 2008
    Realm instances send out notifications to
    other instances on other threads every time a
    write transaction is committed:
    Realm Notifications

    View Slide

  27. Launch iPhone3G
    iOS Developer since mid 2008
    Realm Notifications, e.g.

    View Slide

  28. Launch iPhone3G
    iOS Developer since mid 2008
    Collection Notifications
    Notified of updates to query results or to
    specific objects.

    View Slide

  29. Launch iPhone3G
    iOS Developer since mid 2008
    ( should keep a strong reference )
    Collection Notifications, e.g.

    View Slide

  30. Launch iPhone3G
    iOS Developer since mid 2008
    Fine-grained level changing
    This change parameter describes including the
    indices of objects that have been inserted, deleted, or
    modified since the last notification.

    View Slide

  31. Launch iPhone3G
    iOS Developer since mid 2008
    notified indices with deltas
    Fine-grained change, e.g.

    View Slide

  32. Launch iPhone3G
    iOS Developer since mid 2008
    In the case of Core Data
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: #selector(managedObjectContextDidChange:),
    name: NSManagedObjectContextObjectsDidChangeNotification,

    object: privateManagedObjectContext)
    Can not observe with filtering.

    View Slide

  33. Launch iPhone3G
    iOS Developer since mid 2008
    In the case of Core Data
    func managedObjectContextObjectsDidChange(notification: NSNotification) {
    guard let userInfo = notification.userInfo else { return }
    if let inserts = userInfo[NSInsertedObjectsKey] as? Set where inserts.count > 0 {
    }
    if let updates = userInfo[NSUpdatedObjectsKey] as? Set where updates.count > 0 {
    }
    if let deletes = userInfo[NSDeletedObjectsKey] as? Set where deletes.count > 0 {
    }
    }
    Received changed objects, instead of changed indices

    View Slide

  34. Launch iPhone3G
    iOS Developer since mid 2008
    Notified timing
    When notifications can’t be delivered instantly,
    multiple write transactions may be coalesced into a
    single notification.
    t
    Tx
    COMMIT
    Tx
    COMMIT
    Tx
    COMMIT
    Tx
    COMMIT
    Tx
    COMMIT
    t
    Notice Notice
    Write
    Notification

    View Slide

  35. Launch iPhone3G
    iOS Developer since mid 2008
    It is also notified being monitored
    relation destination of the change of
    the Object.
    Observable relations ( linking objects )

    View Slide

  36. Launch iPhone3G
    iOS Developer since mid 2008
    Observable relations ( linking objects )
    UserObject
    name
    avatar_url
    registered
    deleted_at
    profile
    ProfileObject
    company
    position
    working_histories
    academy_records
    emails
    EmailObject
    email
    Observe Change Change
    Notified

    View Slide

  37. Launch iPhone3G
    iOS Developer since mid 2008
    Model
    View
    B
    View
    A
    Notification
    Notification
    WriteTx
    Absolutely separation logic and views
    Singleton
    PreFetch
    BackgroundFetch

    View Slide

  38. Launch iPhone3G
    iOS Developer since mid 2008
    View
    A
    View
    B
    Notification
    WriteTx
    Absolutely separation logic and views

    View Slide

  39. e.g.
    WriteTx
    Notification
    Notification
    ➜ ➜

    View Slide

  40. Launch iPhone3G
    iOS Developer since mid 2008
    RxSwift
    RxRealm
    x
    meets
    https://github.com/RxSwiftCommunity/RxRealm

    View Slide

  41. Launch iPhone3G
    iOS Developer since mid 2008
    What RxRealm
    Rx wrapper for Realm's collection types.

    View Slide

  42. Launch iPhone3G
    iOS Developer since mid 2008
    Observing collections
    Adds to Results, List, LinkingObjects and AnyRealmCollection
    these methods:
    - asObservable()
    - asObservableArray()
    - asObservableChangeset()
    - asObservableArrayChangeset()
    returning an Observable, RealmChangeset?>

    View Slide

  43. Launch iPhone3G
    iOS Developer since mid 2008
    try! Realm().objects(ContactRealm)
    .asObservableChangeset()
    .subscribeNext { [weak self] results, changeset in
    guard let tableView = self?.tableView else { return }
    self?.results = results
    if let changeset = changeset {
    tableView.beginUpdates()
    tableView.insertRowsAtIndexPaths(…, withRowAnimation: .Automatic)
    tableView.deleteRowsAtIndexPaths(…, withRowAnimation: .Automatic)
    tableView.reloadRowsAtIndexPaths(…, withRowAnimation: .Automatic)
    tableView.endUpdates()
    } else {
    tableView.reloadData()
    }
    }.addDisposableTo(disposeBag)
    e.g.

    View Slide

  44. - rx_add()
    - rx_delete()
    Write transactions
    // Simple Create
    [MessageRealm("hello"), MessageRealm("world")]
    .toObservable()
    .subscribe(Realm.rx_add())
    // With API
    let request = API.Endpoint.MessagesRequest().request()
    Session.rx_response(request)
    .subscribe(Realm.rx_add())

    View Slide

  45. B
    Launch iPhone3G
    iOS Developer since mid 2008
    RxSwift MVVM
    Realm Centered Design
    RxRealm

    View Slide

  46. Launch iPhone3G
    iOS Developer since mid 2008
    View ViewModel
    View A
    Subscribe
    Holding
    - The state of a screen-specific (Requesting, etc)
    - The main data of a screen
    RxSwift w/ MVVM
    It is difficult to handle the screen
    cross-data.
    View B
    View C

    View Slide

  47. Launch iPhone3G
    iOS Developer since mid 2008
    View ViewModel
    Subscribe
    Singleton
    RxSwift w/ MVVM and Singleton
    Global ViewModel
    View A
    View B
    View C
    Holding
    - The state of a screen-specific (Requesting, etc)
    - The main data of a screen
    (Rx Variable)
    The state of cross over screens

    View Slide

  48. Launch iPhone3G
    iOS Developer since mid 2008
    View ViewModel Observable Store
    Subscribe
    The state of a screen-specific
    Singleton
    RxSwift+RxRealm w/
    MVVM
    (RxRealm)
    View A
    View B
    View C
    (Rx Variable)
    The main data of screens
    The state of cross over screens

    View Slide

  49. Conclusion
    Simple
    ( is not easy )
    Complex

    View Slide

  50. 5IBOLZPV

    View Slide