Skip to content

Instantly share code, notes, and snippets.

@abdulowork
Last active April 1, 2017 15:35
Show Gist options
  • Save abdulowork/a93d0b7a323dc447319f0a22bcffe0db to your computer and use it in GitHub Desktop.
Save abdulowork/a93d0b7a323dc447319f0a22bcffe0db to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import PlaygroundSupport
import Foundation
import RealmSwift
import RxSwift
PlaygroundPage.current.needsIndefiniteExecution = true
class RealmString: Object {
dynamic var string: String = ""
}
class RealmStringService {
let realm: Realm
let backgroundScheduler: SchedulerType
init(scheduler: SchedulerType, realm: Realm) {
self.realm = realm
self.backgroundScheduler = scheduler
}
func getStringsFromRealm() -> Observable<[String]> {
return
Observable
.just()
.observeOn(backgroundScheduler)
.map{ [unowned self] in
return self.realm.objects(RealmString.self).map{ $0.string }
}
.observeOn(MainScheduler.instance)
}
}
let realm = try! Realm(configuration: .init(inMemoryIdentifier: "testing"))
//Try changing these and you will see the realm::IncorrectThreadException
//let scheduler = ConcurrentDispatchQueueScheduler(qos: .utility)
let scheduler = MainScheduler.instance
try! realm.write {
let sampleRealmString = RealmString()
sampleRealmString.string = "Test"
realm.add(sampleRealmString)
}
let disposeBag = DisposeBag()
let service = RealmStringService(scheduler: scheduler, realm: realm)
service
.getStringsFromRealm()
.subscribe(onNext:{ print($0) })
.disposed(by: disposeBag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment