GithubHelp home page GithubHelp logo

rxrealm's Introduction

RxRealm

Carthage Compatible Version License Platform

Usage

This library is a thin wrapper around RealmSwift.

Observing collections

RxRealm adds to Results, List, LinkingObjects and AnyRealmCollection these methods:

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?>.

Write transactions

rx_add()

write to existing realm reference) You can add newly created objects to a realm that you already have initialized:

let realm = try! Realm()
[Message("hello"), Message("world")].toObservable()
  .subscribe(realm.rx_add())

Be careful, this will retain your realm until the Observable completes or errors out.

write to the default realm) You can leave it to RxRealm to grab the default Realm on any thread your subscribe and write objects to it:

[Message("hello"), Message("world")].toObservable()
  .observeOn(  ..you can switch threads if you want )
  .subscribe(Realm.rx_add())

write to a specific realm) If you want to switch threads and don't use the default realm, provide a Realm.Configuration:

var conf = Realm.Configuration()
... custom configuration settings ...

[Message("hello"), Message("world")].toObservable()
  .observeOn(  ..you can switch threads if you want )
  .subscribe(Realm.rx_add(conf))

If you want to create yourself the Realm on a different thread than the subscription you can do that too (allows you to error handle):

[Message("hello"), Message("world")].toObservable()
  .observeOn(  ..you can switch threads if you want )
  .subscribeNext {messages in
    let realm = try! Realm()
    try! realm.write {
      realm.add(messages)
    }
  }

rx_delete()

delete from existing realm reference) Delete objects from existing realm reference:

let realm = try! Realm()
realm.objects(Messages).asObservable()
  .subscribe(realm.rx_delete())

Be careful, this will retain your realm until the Observable completes or errors out.

delete automatically from objects' realm) You can leave it to RxRealm to grab the Realm from the first object and use it:

someCollectionOfPersistedObjects.toObservable()
  .subscribe(Realm.rx_delete())

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.

Further you're welcome to peak into the RxRealmTests folder of the example app, which features the library's unit tests.

Installation

This library depends on both RxSwift and RealmSwift 1.0+.

CocoaPods

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

pod "RxRealm"

Carthage

RxRealm is available through Carthage. You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate RxRealm into your Xcode project using Carthage, specify it in your Cartfile:

github "RxSwiftCommunity/RxRealm" ~> 1.0

Run carthage update to build the framework and drag the built RxRealm.framework into your Xcode project.

As Source

You can grab the files in Pod/Classes from this repo and include them in your project.

TODO

  • Test add platforms and add compatibility for the pod

License

This library belongs to RxSwiftCommunity.

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

rxrealm's People

Contributors

atlas-rcacheaux avatar fpillet avatar icanzilb avatar mrackwitz avatar muukii avatar sergdort avatar solidcell avatar tuannguyenanh177 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.