Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm Observables now holds a Realm instance reference #2364

Merged
merged 1 commit into from Mar 7, 2016

Conversation

cmelchior
Copy link
Contributor

Fixes #2357

Due to how RxJava executes the chain of Observables, currently it isn't possible to use doOnUnsubscribe() to close Realm instances as the side-effect is triggered before the Observable is truly unsubscribed to (crashing our 'removeChangeListeners() method).

This PR changes the current implementation so each Observable also holds onto a Realm instance prevent the Realm from being fully closed until the Observable is unsubscribed to. This means we can now guarantee that the Realm is alive for as long as the Observable is subscribed to. The only downside is that you risk leaking Realm instances if you forget to unsubscribe, but that would be a memory leak in any case.

@realm/java
/cc @lucamtudor

@@ -311,7 +336,7 @@ public void call(DynamicRealm rxRealm) {
@UiThreadTest
public void unsubscribe() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change name to unsubscribe_onWrongThread ?

@beeender
Copy link
Contributor

beeender commented Mar 2, 2016

Besides some minor comments, 👍

@beeender
Copy link
Contributor

beeender commented Mar 3, 2016

👍

return Observable.create(new Observable.OnSubscribe<Realm>() {
@Override
public void call(final Subscriber<? super Realm> subscriber) {
// Get instance to make sure that the Realm is open for as long as the
// Observable is subscribed to.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to -> to it

@emanuelez
Copy link
Contributor

just minor comments issues, but they happen in a bunch of places. Otherwise this looks good to me 👍

}
}));

// Immediately call onNext with the current value, as due to Realms auto-update, it will be the latest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Realm's"

cmelchior added a commit that referenced this pull request Mar 7, 2016
Realm Observables now holds a Realm instance reference
@cmelchior cmelchior merged commit 5f6c723 into master Mar 7, 2016
@cmelchior cmelchior deleted the cm/rxjava-close-bug branch March 7, 2016 13:46
@cmelchior cmelchior removed the S:Review label Mar 7, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Realm, RxJava, asObservable() and doOnUnsubscribe()
4 participants