-
-
Save nesterchung/49b0ba61543174f3f8a1119dec703d9e to your computer and use it in GitHub Desktop.
Integration Realm DataBase и kotlinx.coroutines
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
fun getData() = produce<List<Data>>(LooperThread) { | |
var results = realm.where(Data::class.java).findAll() | |
send(map(results)) | |
results.addChangeListener { results -> | |
run { | |
launch(LooperThread) { | |
send(map(results)) | |
} | |
} | |
} | |
} | |
//Rx version used Observable default library implementation like | |
override fun <E : RealmModel> from(realm: Realm, results: RealmResults<E>): Observable<RealmResults<E>> { | |
val realmConfig = realm.configuration | |
return Observable.create { subscriber -> | |
val observableRealm = Realm.getInstance(realmConfig) | |
resultsRefs.get().acquireReference(results) | |
val listener = RealmChangeListener<RealmResults<E>> { | |
if (!subscriber.isUnsubscribed) { | |
subscriber.onNext(results) | |
} | |
} | |
results.addChangeListener(listener) | |
subscriber.add(Subscriptions.create { | |
results.removeChangeListener(listener) | |
observableRealm.close() | |
resultsRefs.get().releaseReference(results) | |
}) | |
subscriber.onNext(results) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment