Created
May 7, 2020 13:29
-
-
Save yunusemredilber/8315787baff7d7ad19794d9d706cc8ba to your computer and use it in GitHub Desktop.
Asynchronous and event-based data passing in Android with RxJava
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
public final class RxBus { | |
// String can be replaced with any kind of Object. | |
private static final BehaviorSubject<String> behaviorSubject | |
= BehaviorSubject.create(); | |
public static Disposable subscribe(@NonNull Consumer<String> action) { | |
return behaviorSubject.subscribe(action); | |
} | |
public static void publish(@NonNull String value) { | |
behaviorSubject.onNext(value); | |
} | |
} | |
/* Usage example | |
// In sender instance | |
RxBus.publish("Yunus says hi!"); | |
// In reciever instance | |
Disposable disposable = RxJsBus.subscribe(value -> { | |
// Do anything with the value | |
}); | |
// In reciver, don't forget to dispose connection when you are done. Example: | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
disposable.dispose(); | |
} | |
*/ | |
// RxJava: https://github.com/ReactiveX/RxJava |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment