Latest 1.0.3
Homepage https://github.com/Urotea/RealmX
License MIT
Platforms ios 12.0
Dependencies RealmSwift, RxSwift, RxCocoa
Frameworks Foundation

RealmX

Codacy Badge
Reviewed by Hound
Carthage compatible

Realm + RxSwift eXtensions
This is a micro library wrapping aroud RealmSwift and RxSwift.

What you can do using this

ReamX can observe Realm Mobile DataBase using RxSwift.
And more, you can write or delete object in reactive stream.

RealmX provides three APIs.

Results<Element>.toObservable() -> Observable<Results<Element>>
Realm().doInTransaction<Element: Object>(
        object: [Element],
        inTransaction: @escaping ((Realm, [Element]) -> Void)) -> Completable
doInTransaction<Element: Object>(
        results: Results<Element>,
        inTransaction: @escaping ((Realm, Results<Element>) -> Void)) -> Completable

Installation

Carthage

  • Add github "Urotea/RealmX" to your Cartfile.
  • Run carthage update

CocoaPods

  • Add pod ‘Realm-rX’ to your Podfile.
  • Run pod update

samples

toObservable

This API emits a event when Realm records changed.

let realm = try! Realm()
realm.objects(Dog.self).toObservable().subscribe{
    // when Dog table changed, this print is called.
    print($0)
}

You can make a stream on background thread.

DispatchQueue.global(qos: .background).async {
    autoreleasepool {
        let realm = try! Realm()
        realm.objects(Dog.self).toObservable().subscribe {
            print($0)
        }
    }
}

doInTransaction

This API provides Realm transaction on any stream.
In particular, this is useful for getting json from the network and writing it to Realm.

let realm = try! Realm()
let dog = Dog()
dog.age = 1
dog.name = "pochi"

realm.doInTransaction(object: [dog]) { (realm: Realm, dogList: [Object]) in 
     realm.add(dogList.first!)
}.subscribe(onCompleted: {
    print("insert success.")
})

This show the most power when you use WebAPI(e.g. Moya) in Rx stream.

let provider = MoyaProvider<Dogs>()
provider
    .rx
    .request(.get)
    .observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
    .map{
        let dog = Dog()
        dog.age = $0.age
        dog.name = $0.name
    }
    .flatMap {
        let realm = try! Realm()
        return realm.doInTransaction(object: [$0]) { realm, dogList in
            realm.add(dogList.first!)
        }
    }
    .subscribe {}

Example App

To run example app, clone this repository and run carthage bootstrap.
And then, you can run exsample app.
Further, if you look RealmXTests directory, you can more understand how to use.

Requirements

iOS 12.0 or later

License

RealmX is available under the MIT license.

Latest podspec

{
    "name": "Realm-rX",
    "version": "1.0.3",
    "summary": "Realm + RxSwift eXtensions",
    "swift_version": "4.0",
    "homepage": "https://github.com/Urotea/RealmX",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": "Urotea",
    "platforms": {
        "ios": "12.0"
    },
    "source": {
        "git": "https://github.com/Urotea/RealmX.git",
        "tag": "1.0.3"
    },
    "source_files": "RealmX/**/*.swift",
    "frameworks": "Foundation",
    "dependencies": {
        "RealmSwift": [
            ">= 3.13.0"
        ],
        "RxSwift": [
            ">= 4.4.0"
        ],
        "RxCocoa": [
            ">= 4.4.0"
        ]
    }
}

Pin It on Pinterest

Share This