Skip to content

Latest commit

 

History

History
97 lines (69 loc) · 2.79 KB

README.md

File metadata and controls

97 lines (69 loc) · 2.79 KB

RxRealm

Version License Platform

Usage

This library is a very thin wrapper around the reactive collection types RealmSwift provides: Results, List and AnyRealmCollection.

The extension adds these methods to all of the above:

asObservable()

asObservable() - emits every time the collection changes:

let realm = try! Realm()
realm.objects(Lap).asObservable()
  .map {laps in "\(laps.count) laps"}
  .subscribeNext { text  in
    print(text)
  }

asObservableArray()

asObservableArray() - fetches the a snapshot of a Realm collection and converts it to an array value (for example if you want to use array methods on the collection):

let realm = try! Realm()
realm.objects(Lap).asObservableArray()
  .map {array in
    return array.prefix(3) //slice of first 3 items
  }
  .subscribeNext { text  in
    print(text)
  }

asObservableChangeset()

asObservableChangeset() - emits every time the collection changes and provides the exact indexes that has been deleted, inserted or updated:

let realm = try! Realm()
realm.objects(Lap).asObservableChangeset()
  .subscribeNext {result, changes in
    if let changes = changes {
	  //it's an update
	  print(result)
	  print("deleted: \(changes.deleted) inserted: \(changes.inserted) updated: \(changes.updated)")
	} else {
	  //it's the initial data
	  print(result)
	}
  }

asObservableArrayChangeset()

asObservableArrayChangeset() combines the result of asObservableArray() and asObservableChangeset() returning an Observable<Array<T>, RealmChangeset?>.

Example app

To run the example project, clone the repo, and run pod install from the Example directory first. The app uses RxSwift, RxCocoa using RealmSwift, RxRealm to observe Results from Realm.

Installation

This library depends on both RxSwift and RealmSwift 0.99+.

CocoaPods

RxRealm is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "RxRealm"

Carthage

Feel free to send a PR

As Source

You can grab the RxRealm.swift file from this repo and include it in your project.

Author

This library belongs to RxSwiftCommunity and is based on the work of @fpillet

TODO

  • Carthage
  • Add asObservable() to the Realm class
  • Test add platforms and add compatibility for the pod
  • Document the source code

License

RxRealm is available under the MIT license. See the LICENSE file for more info.