Created
October 20, 2016 21:18
-
-
Save carloe/2fc5883cc37088bd6fa29928151bee61 to your computer and use it in GitHub Desktop.
Observe Realm changes with RxSwift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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