简体   繁体   中英

Android rxjava retrofit chain calls not working

List item

I very new to RXJava iam using retrofit to make api via Rxjava flatmaps calls in android. My auth activity divided in three parts in

  1. First part make call to login.
  2. After successful login get user previous details if he is existing user.
  3. After then update firebase fcm token to database.

But here only first flatmap only executed remaining flatmap not executing

here is my code

 Auth auth = new Auth();
                auth.setEmail(email);
                auth.setPassword(password);
                authUser.createUser(auth)
                       .flatMap( response -> {
                        //Only this block is executed
                           Log.d("FlatMap1", String.valueOf(response.code()));
                           if(response.code() == 200) {
                               String token = response.body().getToken();
                               editor.putString("token", token);
                               editor.apply();

                               UserId getUser = new UserId();
                               getUser.setUid(firebaseAuth.getUid());
                               return userInfo.CurrentUser("barer " + sharedPreferences.getString("token", null), getUser);

                           }else return null;
                       })
                        .flatMap( response -> {
                           if(!isNew){
                           Log.d("FlatMap1", String.valueOf(response.code()));
                           if(response.code() == 200) {
                              UserInfo userInfo = response.body.UserInfo();

                           }else return null;
                        }else{
                         UpdateFCM updateFCM = new UpdateFCM();
                         updateFCM.setToken(fcmToken);
                         return  updateFcmToken.update("barer 
"+sharedPreferences.getString("token",null),updateFCM);
                        }
                       }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new Observer<Response<Void>>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Response<Void> voidResponse) {
                                Log.d("FlatMap3",String.valueOf(voidResponse.code()));
                                if(voidResponse.code() == 200){
                                    Toast.makeText(getApplicationContext(),"Login success",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });

So what was wrong and don't make duplicate and down vote;

You are not allowed to return null values in operator chain from RxJava2. Since you are returning null , the whole abruptly breaks and no new elements would be pushed down the stream.

Read this for reference - RxJava 2.0 does not support Null Values

you have to return empty observable instead of return null.

if(response.code() == 200) {
   String token = response.body().getToken();
   editor.putString("token", token);
   editor.apply();

   UserId getUser = new UserId();
   getUser.setUid(firebaseAuth.getUid());
   return userInfo.CurrentUser("barer " + sharedPreferences.getString("token", null), getUser);

    } else {

    return Observable.empty();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM