1

I would like to hide my Realm implementation and instead of working on RLMNotificationBlock I would like to use RXSwift. Below how my method looks like now (RLMNotificationBlock is a block that takes String and RLMRealm):

func addNotificationBlock(block: RLMNotificationBlock) -> RLMNotificationToken? {
    let rlmObject = ...
    return rlmObject.addNotificationBlock(block)
}

But I would like to switch to more reactive observer-pattern way. I looked at RxSwift docs and source code of rx_clickedButtonAtIndex, but I cannot figure out how I should put all these things together. I guess my code at the end would look like:

public var rx_realmContentChanged: ControlEvent<Int> {
    let controlEvent = ControlEvent()
    // My code go here
    return controlEvent
}

I'm new with RXSwift and know only the basics. Any help will be appreciated.

4

2 に答える 2

1

GitHub で使用できる Rx Realm 拡張機能があります: https://github.com/RxSwiftCommunity/RxRealm

単一の Realm オブジェクトまたは Realm Collection から Observable を取得できます。README の例を次に示します。

let realm = try! Realm()
let laps = realm.objects(Lap.self))

Observable.changesetFrom(laps)
  .subscribe(onNext: { results, changes in
    if let changes = changes {
    // it's an update
    print(results)
    print("deleted: \(changes.deleted) inserted: \(changes.inserted) updated: \(changes.updated)")
  } else {
    // it's the initial data
    print(results)
  }
})

RxRealmDataSources と呼ばれるテーブルとコレクション ビューをバインドするために特別に構築された追加のライブラリもあります。

于 2017-01-03T11:44:49.933 に答える