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

RxQuery violates observable contract #687

Closed
MrSlateZB opened this issue Apr 4, 2019 · 3 comments
Closed

RxQuery violates observable contract #687

MrSlateZB opened this issue Apr 4, 2019 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@MrSlateZB
Copy link

The observable contract doesn't allow for concurrent calls of onNext/onComplete/or onError. ObjectBox subscriptions can come in on multiple threads concurrently if the database is changing rapidly enough. This would cause onNext to be called concurrently from multiple threads.

Thankfully, the emitter from RxJava has a .serialize() method that you can use to make the emitter safe for this use case. This should fix the issue.

@greenrobot-team
Copy link
Member

Thanks!

Verified this, quote from docs:

Note that the onNext(T), onError(java.lang.Throwable) and onComplete() methods provided to the function via the Emitter instance should be called synchronously, never concurrently. Calling them from multiple threads is not supported and leads to an undefined behavior.
http://reactivex.io/RxJava/javadoc/io/reactivex/Emitter.html

Side note: ObjectBoxLiveData is safe as it uses LiveData#postValue which posts a task to the main thread.

For "regular" observers this is solved by passing a scheduler, e.g.:

query.subscribe(subscriptions)
     .on(AndroidScheduler.mainThread())
     .observer(data -> updateUi(data));

Side note: should update the query observers docs to always explicitly specify a scheduler.
https://docs.objectbox.io/data-observers-and-rx#observing-queries

-Uwe

@greenrobot-team greenrobot-team added the bug Something isn't working label Apr 8, 2019
@greenrobot-team greenrobot-team self-assigned this Apr 8, 2019
@MrSlateZB
Copy link
Author

If the serialized emitter fix isn't going to be implemented, I would just make it very clear that any multi-threaded scheduler would still recreate this problem.

@MrSlateZB
Copy link
Author

In #793 emissions seem to now be synchronized onto a single thread. Because of this, the observable contract shouldn't be violated anymore.

@greenrobot-team greenrobot-team added this to the 3.0 milestone Mar 30, 2020
@greenrobot-team greenrobot-team modified the milestones: 3.0, 2.9.2 Oct 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants