I am having problems with EventBus 3.0.0 where I post a single event like this:
Call<List<SessionSpec>> call = httpService.getSessionSpecs();
call.enqueue(new Callback<List<SessionSpec>>() {
#Override
public void onResponse(Call<List<SessionSpec>> call, Response<List<SessionSpec>> response) {
if (response.isSuccessful()) {
List<SessionSpec> specs = response.body();
EventBus.getDefault().post((List<SessionSpec>)specs);
}
else Log.e(TAG, "sendSessionSpecs(): request NOT successful");
}
#Override
public void onFailure(Call<List<SessionSpec>> call, Throwable t) {
Log.e(TAG, "sendSessionsSpecs(): failed");
}
});
I have two subscribers in the same fragment, each with different signatures, but they are both getting called from a single post:
#Subscribe
public void onSessionSpec(List<SessionSpec> specs) {
Log.d(TAG, "onSessionSpec(): entered");
Log.d(TAG, " : number of session specs: " + specs.size());
}
The second subscriber is defined as:
#Subscribe
public void onOverlayType(List<OverlayType> types) {
Log.d(TAG, "onOverlayType(): entered");
Log.d(TAG, " : number of overlay types: " + types.size());
}
Both of these callbacks are in a single fragment which is active when the post is done and I have verified that the post is only called once. When the single SessionSpec event is posted, both the onSessionSpec and the onOverlayType callbacks are dispatched by EventBus with the event type of List> so the onOverlayType callback receives the wrong type in its callback argument. The class OverlayType is a simple POJO class with 2 members, a int "sid" and a String "name". The class SessionSpec is more complex; it does have a member String "name" but other than that, nothing else is common between these 2 classes. I have verified that there is nothing closely resembling "OverlayType" in the SessionSpec class.
The interface definition is this:
public interface VcapHttpInterface {
#GET("overlay/types")
Call<List<OverlayType>> getOverlayTypes();
#GET("session/list")
Call<List<SessionSpec>> getSessionSpecs();
#GET("session/{id}")
Call<Session> getSession(#Path("id") int sid);
}
The getSession event post/callback has no problems.
I have spend all day trying to figure what is going wrong so I am clueless at this point. Anybody know what might be wrong with my code?
Thanks,
-Andres
Edit: How does EventBus know which handler to call for a particular response? Some posts I have read said that EventBus does not use the handler signature, but how else would it know how to map a response to the right subscribed handler routine? Is there a way to explicitly define the handler callback for a given event?
EventBus checks the class of the object that you are posting, and calls the methods that expect that class in their parameters. In your case you are posting an object which is a List. In both your listeners you expect an object of type List. It doesn't matter what generic you put in OverlayType or SessionSpec, eventbus will call both. In order to make it work you gotta define to models as events.
public class OverlayTypeEvent {
public List<OverlayType> types;
public OverlayTypeEvent(List<OverlayType> types) {
this.types = types;
}
}
and
public class SessionSpecEvent {
public List<SessionSpec> types;
public SessionSpecEvent(List<SessionSpec> types) {
this.types = types;
}
}
And listen on them seperatley. Then post events with the specific type.
#Subscribe
public void onSessionSpec(OverlayTypeEvent event) {
List<OverlayType> overlayTypes = event.overlayType;
}
If you don't want to create new class as a container everytime you send a list data, you can you Pair as simple container, it has two generic fields (first and second) to contain variables.
You can use first as a key to check the type of class, second contains the actually data.
List<SessionSpec> specs = response.body();
EventBus.getDefault().post(new Pair<>(SessionSpec.class.getSimpleName(), specs));
Receive data:
#Subscribe
public void onSessionSpec(Pair<String, List<SessionSpec>> specContainer){
if (SessionSpec.class.getSimpleName().equals(specContainer.first)) {
List<SessionSpec> sessionSpecs = specContainer.second;
}
}
#Subscribe
public void onOverlayType(Pair<String, List<OverlayType>> overlayContainer) {
if (OverlayType.class.getSimpleName().equals(overlayContainer.first)) {
List<OverlayType> overlayTypes = overlayContainer.second;
}
}
Advantage of this solution: Reduce creating unneeded classes.
Disadvantage: both onSessionSpec and onOverlayType get called.
Related
New to RxJava and I have question about interface callbacks ( called from inner layer/module of code through interface variable) vs RxJava.
To make it more clear, quick example:
Standard callback interface implementation, interface, class A and B
interface CustomCallback {
void onCallbackCalled(String str);
}
class ClassA {
private ClassB classB;
public ClassA() {
classB = new ClassB(new CustomCallback() {
#Override
public void onCallbackCalled(String str) {
System.out.println("Callback called " + str);
}
});
}
}
class ClassB {
private CustomCallback customCallback;
public ClassB(CustomCallback callback) {
customCallback = callback;
}
private void somethingHappened() {
customCallback.onCallbackCalled("method somethingHappened");
}
}
When classB method "somethingHappened" is called, result is: "Callback called method somethingHappened".
Interface's method onCallbackCalled(String str) can be called from classB as many times as I want.
CLASS A ↓ ............................................ injection of interface through constructor
CLASS B................↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ onCallbackCalled(...) 0...n number
Now RxJava. 99% of cases which I find.
class ClassA {
private ClassB classB;
public ClassA() {
classB = new ClassB();
}
public void rxJavaMethod() {
DisposableObserver<String> observer = classB.getObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
#Override
public void onNext(String s) {}
#Override
public void onError(Throwable e) {}
#Override
public void onComplete() {}
});
}
}
class ClassB {
private Observable<String> getObservable() {
return Observable.just(can be different from "just", for sake of example);
}
}
Scheme is:
CLASS A ↓........................ one call for getting Observable resource
CLASS B................↑ EDIT returns observable which emits 0...n values
So basically you call from top layer ( in this example) and you get response about state from inner layer.
QUESTIONS:
1) What in case when you have a model ( inner layer) which is changing dynamically ( but not any kind of AsyncTask etc.), and you want to notify top layer ( UI for example) that state has changed ( good example: game).
2) Is there any kind of "bridge" class in RxJava library ( I think about it as "subscribe to it, then you can pass arguments to it as many times as you want and information/observable will be emitted to subscribers).
3) Is there any sense and advanatage of trying to do that instead of standard interface callbacks ( in case like above, not " click button, get response once")
UPDATE, ANSWER BASED ON EXAMPLE ABOVE
As Bob Dalgleish mentioned, way of making such bridge is by using one of the class extending Subject<T> rxjava.
http://reactivex.io/documentation/subject.html
class ClassA {
private ClassB classB;
public ClassA() {
classB = new ClassB();
}
public void rxJavaMethod() {
DisposableObserver<String> observer = classB.getCallbackSubjectRx()
.subscribeWith(new DisposableObserver<String>() {
#Override
public void onNext(String s) {}
#Override
public void onError(Throwable e) {}
#Override
public void onComplete() {}
});
}
}
class ClassB {
private BehaviorSubject<String> mCallbackRx;
public ClassB() {
mCallbackRx = BehaviorSubject.create();
}
// method somethingHappened can be invoked whenever whe want and
// it will send given parameter to all subscribers
private void somethingHappened() {
mCallbackRx.onNext("method somethingHappened");
}
// multiple subscribers allowed
public BehaviorSubject<String> getCallbackSubjectRx() {
return mCallbackRx;
}
}
Downside might be, that if we want to use one "bridge" to handle multiple callback types ( interface have methods, we use only one method: "onNext()"), we might need to create wrapper class with callback parameters. Which isn't big problem in my opinion.
On the other hand, we get access to all of RxJava operators.
http://reactivex.io/documentation/operators.html
( Example above is for RxJava2, where Disposable is basically Subscription from RxJava1).
The first thing to note is that
CLASS B................↑ returns 0...n observables to observer
is not true. Class B returns an observable, on which it will occasionally emit 0..n values.
(the question is not clear). The inner observable, from class B, is changing state for whatever reason. The most common reason is that another process/task/thread is feeding it, and you want to display the resulting state in the UI.
A simple type of "bridging" class that I use all the time any of the several Subject<> classes. You can emit new values to them using .onNext() and the subscribers will get those values.
If callback interfaces were all standardized, then they would have some advantage, but they vary all over the place. You have to remember that you need some particular interface for this thing you are looking at and and a different one for the other thing. While UI events tend to be quite uniform these days, trying to mix UI events and network events and database events will still leave you feeling overwhelmed. Having a much smaller class of interfaces, mostly encapsulated inside of the rxJava generic classes, makes composing functionality much easier.
Edit: Improve example code.
There is a good article from Yammer Engineering on using Observable.create() (formerly Observable.fromEmitter(), formerly Observable.fromAsync(). The important points he makes are
Using Observable.create() handles the subscription step for you by registering a listener to the underlying interface. More importantly, it arranges to de-register the listener when the unsubscribe() occurs.
Out of the box, this code handles multiple subscribers, each of which receives its own observable stream of data.
As I mentioned above, the listener protocol is particular to the thing you register with. If that thing supports only a single listener, then you will likely want to introduce a Subject that subscribes to the thing under observation, and all your other observers subscribe to the subject.
End of edit.
My favorite example of composition of solutions is the distinctUntilChanged() operator. Because it is an operator that works on a generic observable, it encapsulates the stateful property of saving consecutive values for comparison and only emitting differing ones. I use it frequently for logging state changes. To achieve the same end using standard callback interfaces would require adding a different interface for saving prior values to every existing interface.
So, yes, most of the time it is worth using the rxJava approach of observables, simply for the sake of not having to remember which of the many call back protocols might be applicable in the current case.
Before using rx.Observable, I used a custom callback with retrofit so I can add some specific logic for handling response/error and not have to do that inside the callback for every request as boilerplate code.
I force users to use the custom callback by putting it in the method signature like this:
#GET("/user_endpoint/")
void getUser(CustomCallback<User> callback);
#GET("/profile_endpoint/")
void getProfile(CustomCallback<Profile> callback);
but now that I'm returning an Observable:
#GET("/user_endpoint/")
Observable<User> getUser();
#GET("/profile_endpoint/")
Observable<Profile> getProfile();
I can't figure out a way to make sure that a custom callback always proxies the error/response.
Also, with retrofit2.0, how can I force the user to use a custom callback with the returned Call object?
CustomCallback for reference:
public abstract class CustomCallback<T> implements Callback<T> {
#Override public final void success(T t, Response response) {
// do some logic
onSuccess(t);
}
#Override public final void failure(RetrofitError error) {
// do something with the error here such as show a Toast
Toast.makeText(Application.getInstance(), error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
onFailure(error);
}
public abstract void onSuccess(T response);
public abstract void onFailure(Throwable error);
}
Stop. You're thinking this the wrong way.
Instead consider this: You have the normal Retrofit interface:
interface Foo {
#GET("/user_endpoint/")
Observable<User> getUser();
}
And then you have your decorator class:
public class FooDecorator implements Foo {
private Foo delegate = ...; // inject or create the Retrofit instance.
#Override
public Observable<User> getUser() {
return delegate.getUser().doOnNext(...).doOnError(...);
}
}
Then you use only the second class everywhere in your code (preferably just let the DI system use that) and you're set.
If you're feeling adventurous, you could even adapt the RxJavaCallAdapterFactory so that it modifies the returned observables without the need of a custom class.
I am new in Android and Retrofit and I am facing one problem.
I want to have my lets say "ServerCommunication" class (singelton) where all Retrofit magic is done and it will have public methods where REST calls are done.
I want to use this "ServerCommunication" instance in my activities to call Rest service, but thats it. Application logic should be done in activity. So this way some activity Login calls method Login(POJORequest) in "ServerCommunication) where actual REST call via Retrofit framework is done and some POJOResponse is returned. So Activity doesn't care about REST communication while ServerCommunication doesn't care about what logic that should be applied to response from REST service since.
With retrofit 2 I do not understand how I can block Activity to wait for response from retrofit and how it can be returned. Well, I might think I can use some callback methods in activity so those methods can be called from ServerCommunication" in OnPostExecute() to apply some logic based on data from response. It's just I think it should be simpler approach.
Well, to clarify all this mess above imagine simple case: You have data in you main activity, you pass this data to your communication class where REST call is done and response is received. This response must be validated in order to continue. And you want this validation to be done in main activity and NOT in communication class.
What is pattern to do that in Android with Retrofit2 ?
Thank you in advance
What I normally do:
Create your Interface (where you have all your REST methods - GET & POST etc)
Create a class that does the actual calls with corresponding methods (refer to the interface REST methods). I would call it something like ServiceAPIImplementor. This is where you actually create your Retrofit adapter.
In your activity, create an instance of your implementor class and call the methods and pass the expected arguments.
After calling the methods, you should probably show a progress dialog to let the user know that something is going on.
When the onResponse or onFailure method is called, use an Event pattern (EventBus library?) to notify the activity that the network operation has been completed. Once the activity has received the notification, it should then dismiss the progress dialog and update the UI accordingly - with the newly received data or completed operation (expected outcome).
I hope this helps you get closer to what you are trying to achieve!
Service interface (IPhotoService):
#GET("/photos/kudos")
Call<String> fetchKudos(#Header("Authorization") String authorization,
#Query("offset") int offset, #Query("mt") boolean mt);
Service impl (PhotoService):
private GoApiProvider<IPhotoService> mGoProvider = new GoApiProvider<>();
public Promiser<List<Photo>, HttpError> fetchKudos() {
return new Promiser<>((resolve, reject) ->
mGoProvider.getService(IPhotoService.class).fetchKudos(mSession.getToken(),
mOffsetKudos, true).enqueue(new Callback<String>() {
#Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
PhotoParser JSON = new PhotoParser();
try {
mOffsetKudos = mOffsetKudos + 20;
resolve.run(JSON.photosFromJson(response.body()));
} catch (JSONException e) {
Log.e("fetchKudos", e.toString());
}
} else {
reject.run(new HttpError(response.code(), response.message()));
}
}
#Override
public void onFailure(Call<String> call, Throwable t) {
reject.run(new HttpError(YPErrorType.Undefined.getType(), t.getMessage()));
}
})
);
}
Activity or Fragment :
private void loadPhoto() {
new PhotoService().fetchKudos()
.success(this::resultSucceeded)
.error(this::resultError);
}
private void resultSucceeded(List<Photo> photos) {
mPhotoAdapter.setItems(photos);
}
private void resultError(HttpError httpError) {
httpErrorToast(httpError);
}
If you want to use Promizer: Click here
I'm start learning RxJava and I like it so far. I have a fragment that communicate with an activity on button click (to replace the current fragment with a new fragment). Google recommends interface for fragments to communicate up to the activity but it's too verbose, I tried to use broadcast receiver which works generally but it had drawbacks.
Since I'm learning RxJava I wonder if it's a good option to communicate from fragments to activities (or fragment to fragment)?. If so, whats the best way to use RxJava for this type of communication?. Do I need to make event bus like this one and if that's the case should I make a single instance of the bus and use it globally (with subjects)?
Yes and it's pretty amazing after you learn how to do it. Consider the following singleton class:
public class UsernameModel {
private static UsernameModel instance;
private PublishSubject<String> subject = PublishSubject.create();
public static UsernameModel instanceOf() {
if (instance == null) {
instance = new UsernameModel();
}
return instance;
}
/**
* Pass a String down to event listeners.
*/
public void setString(String string) {
subject.onNext(string);
}
/**
* Subscribe to this Observable. On event, do something e.g. replace a fragment
*/
public Observable<String> getStringObservable() {
return subject;
}
}
In your Activity be ready to receive events (e.g. have it in the onCreate):
UsernameModel usernameModel = UsernameModel.instanceOf();
//be sure to unsubscribe somewhere when activity is "dying" e.g. onDestroy
subscription = usernameModel.getStringObservable()
.subscribe(s -> {
// Do on new string event e.g. replace fragment here
}, throwable -> {
// Normally no error will happen here based on this example.
});
In you Fragment pass down the event when it occurs:
UsernameModel.instanceOf().setString("Nick");
Your activity then will do something.
Tip 1: Change the String with any object type you like.
Tip 2: It works also great if you have Dependency injection.
Update:
I wrote a more lengthy article
Currently I think my preferred approach to this question is this to:
1.) Instead of one global bus that handles everything throughout the app (and consequently gets quite unwieldy) use "local" buses for clearly defined purposes and only plug them in where you need them.
For example you might have:
One bus for sending data between your Activitys and your ApiService.
One bus for communicating between several Fragments in an Activity.
One bus that sends the currently selected app theme color to all Activitys so that they can tint all icons accordingly.
2.) Use Dagger (or maybe AndroidAnnotations if you prefer that) to make the wiring-everything-together a bit less painful (and to also avoid lots of static instances). This also makes it easier to, e. g. have a single component that deals only with storing and reading the login status in the SharedPreferences - this component could then also be wired directly to your ApiService to provide the session token for all requests.
3.) Feel free to use Subjects internally but "cast" them to Observable before handing them out to the public by calling return subject.asObservable(). This prevents other classes from pushing values into the Subject where they shouldn't be allowed to.
Define events
public class Trigger {
public Trigger() {
}
public static class Increment {
}
public static class Decrement {
}
public static class Reset {
}
}
Event controller
public class RxTrigger {
private PublishSubject<Object> mRxTrigger = PublishSubject.create();
public RxTrigger() {
// required
}
public void send(Object o) {
mRxTrigger.onNext(o);
}
public Observable<Object> toObservable() {
return mRxTrigger;
}
// check for available events
public boolean hasObservers() {
return mRxTrigger.hasObservers();
}
}
Application.class
public class App extends Application {
private RxTrigger rxTrigger;
public App getApp() {
return (App) getApplicationContext();
}
#Override
public void onCreate() {
super.onCreate();
rxTrigger = new RxTrigger();
}
public RxTrigger reactiveTrigger() {
return rxTrigger;
}
}
Register event listener wherever required
MyApplication mApp = (App) getApplicationContext();
mApp
.reactiveTrigger() // singleton object of trigger
.toObservable()
.subscribeOn(Schedulers.io()) // push to io thread
.observeOn(AndroidSchedulers.mainThread()) // listen calls on main thread
.subscribe(object -> { //receive events here
if (object instanceof Trigger.Increment) {
fabCounter.setText(String.valueOf(Integer.parseInt(fabCounter.getText().toString()) + 1));
} else if (object instanceof Trigger.Decrement) {
if (Integer.parseInt(fabCounter.getText().toString()) != 0)
fabCounter.setText(String.valueOf(Integer.parseInt(fabCounter.getText().toString()) - 1));
} else if (object instanceof Trigger.Reset) {
fabCounter.setText("0");
}
});
Send/Fire event
MyApplication mApp = (App) getApplicationContext();
//increment
mApp
.reactiveTrigger()
.send(new Trigger.Increment());
//decrement
mApp
.reactiveTrigger()
.send(new Trigger.Decrement());
Full implementation for above library with example -> RxTrigger
I am looking for example where I can call loopback's custom method from Android. To explain more, lets say I have a method on server side with name "greet(name)" that will greet someone. I want to invoke that from Android. Any example, or link is ok.
Thanks in advance.
Jahid
In the examples below, I'll assume your model is called Greeter and the static method Greeter.greet is invoked via GET /greeters/greet?name=Alex.
First of all, you need to describe the REST mapping of your method. Then you can call the method using invokeMethod.
public class GreeterRepository extends ModelRepository<Greeter> {
public RestContract createContract() {
RestContract contract = super.createContract();
contract.addItem(new RestContractItem("/" + getNameForRestUrl() + "/greet", "POST"),
getClassName() + ".greet");
return contract;
}
public void greet(name, final VoidCallback callback) {
invokeStaticMethod("greet", ImmutableMap.of("name", name), new Adapter.Callback() {
#Override
public void onError(Throwable t) {
callback.onError(t);
}
#Override
public void onSuccess(String response) {
callback.onSuccess();
}
});
}
}
See ModelRepository.java and Model.java for examples of methods that parse the response body.
Disclaimer: I am one of the developers of LoopBack, loopback-sdk-android is one of my specialisations.