Skip to content

Instantly share code, notes, and snippets.

@carloe
Created October 20, 2016 21:18
Show Gist options
  • Save carloe/2fc5883cc37088bd6fa29928151bee61 to your computer and use it in GitHub Desktop.
Save carloe/2fc5883cc37088bd6fa29928151bee61 to your computer and use it in GitHub Desktop.
Observe Realm changes with RxSwift
//
// Realm+RxSwift.swift
//
// Created by Carlo Eugster on 18/06/16.
// Copyright © 2016 relaunch. All rights reserved.
//
import Foundation
import RealmSwift
import RxSwift
extension Results {
/**
Observable Realm Results that sends the change notification to subscribers once on query, then
again every time a change occurs in the Results.
*/
func asObservable() -> Observable<RealmCollectionChange<Results<Element>>> {
return Observable.create { observer in
var token: NotificationToken? = nil
token = self.addNotificationBlock({ change in
observer.onNext(change)
})
return AnonymousDisposable {
token?.stop()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment