title: Observing Multiple LiveData and MVVM Structure Android Java
body: <p>I will try to make this question general as much as I can.</p>
  <p>In short, I'm using FireStore Firebase and what I am trying to do is that I have Two Repositories (TransactionsRepository and VaultAccountRepository) and each one has some queries to add (Transaction or VaultAccount objects), edit, delete, etc...</p>
  <p>I made a third Repository (OperationRepository) in which I want to (for example) get all transactions and get the default account to do an operation on them (like adding all the transactions amount with the account initial value and so on) and get that operation as alive data to observe it in the view.</p>
  <p>The idea here is that I don't know how to observe the transactions and the default account to update the operation returned and consequently update the UI</p>
  <p>I have read about <code>Transformation.map</code> and <code>Transformation.switchMap</code>, but I could not understand them at all and most examples are in Kotlin and I'm not even sure that it's what I'm looking for to do this! and not sure if this structure is done right as I'm new to MVVM.</p>
  <p>I will include some of the code to show what I have tried to do just in case my explanation above wasn't clear enough.</p>
  <pre><code>public class VaultRepository {
  ...
  private final MutableLiveData&lt;VaultAccount&gt; defaultVaultLiveData;
  
  ...
  
  public MutableLiveData&lt;VaultAccount&gt; getDefaultVault() {
          fireStore.collection(&quot;users&quot;).document(firebaseUser.getUid()).collection(&quot;vaults&quot;)
                  .whereEqualTo(&quot;defaultAccount&quot;, true)
                  .addSnapshotListener((value, error) -&gt; {
                      VaultAccount vaultAccount = new VaultAccount();
                      if (value != null) {
                          for (QueryDocumentSnapshot doc : value) {
                              if (doc != null) {
                                  vaultAccount = doc.toObject(VaultAccount.class);
                              }
                          }
                          defaultVaultLiveData.postValue(vaultAccount);
                      }
                  });
          return defaultVaultLiveData;
      }
  
  
  }
  </code></pre>
  <pre><code>public class TransactionRepository {
  
  ...
  
  private final MutableLiveData&lt;List&lt;Transaction&gt;&gt; allTransactionsLiveData;
  
  ...
  
  public MutableLiveData&lt;List&lt;Transaction&gt;&gt; getAllTransactions() {
          fireStore.collection(&quot;users&quot;).document(firebaseUser.getUid())
                  .collection(&quot;transactions&quot;)
                  .orderBy(&quot;transactionDate&quot;)
                  .addSnapshotListener((value, error) -&gt; {
                      List&lt;Transaction&gt; transactionList = new ArrayList&lt;&gt;();
                      if (value != null) {
                          for (QueryDocumentSnapshot doc : value) {
                              if (doc != null) {
                                  Transaction transaction = doc.toObject(Transaction.class);
                                  transactionList.add(transaction);
                              }
                          }
                          allTransactionsLiveData.postValue(transactionList);
                      }
                  });
          return allTransactionsLiveData;
      }
  
  
  }
  </code></pre>
  <p><em>old OperationRepository</em></p>
  <pre><code>public class OperationRepository {
  
      private final Application mApplication;
      private TransactionRepository transactionRepository;
      private VaultRepository vaultRepository;
      private MutableLiveData&lt;String&gt; stringMutableLiveData;
  
      public OperationRepository(Application mApplication) {
          this.mApplication = mApplication;
          transactionRepository = new TransactionRepository(mApplication);
          vaultRepository = new VaultRepository(mApplication);
          stringMutableLiveData = new MutableLiveData&lt;&gt;();
      }
  
      public MutableLiveData&lt;String&gt; getFinalAmount() {
          
          //TODO get all the transactions and get there amount and observe it
          // get the account initial amount and observe it
          //add all the values and present it in a String LiveData to be shown in the view
  
  
          double result = 0;
  
          String finalResult = String.valueOf(result);
          stringMutableLiveData.postValue(finalResult);
          return stringMutableLiveData;
      }
  
  
  }
  </code></pre>
  <hr />
  <p><strong>UPDATE:</strong>
  I changed my OperationRepository to OperationVeiwModel and tried to use <code>Transformation.switchMap</code>.</p>
  <pre><code>public class OperationViewModel extends AndroidViewModel {
  
      private final TransactionRepository transactionRepository;
      private final VaultRepository vaultRepository;
  
      private MutableLiveData&lt;List&lt;Transaction&gt;&gt; transactionListLiveData;
      private MutableLiveData&lt;VaultAccount&gt; vaultAccountLiveData;
      private LiveData&lt;Double&gt; transactionTotal;
      private LiveData&lt;Double&gt; accountAmount;
      LiveData&lt;String&gt; totalAmount;
  
      public OperationViewModel(@NonNull Application application) {
          super(application);
          transactionRepository = new TransactionRepository(application);
          vaultRepository = new VaultRepository(application);
  
          transactionListLiveData = new MutableLiveData&lt;&gt;();
          vaultAccountLiveData = new MutableLiveData&lt;&gt;();
      }
  
      double result = 0;
  
      public LiveData&lt;String&gt; getFinalAmount() {
          transactionListLiveData = transactionRepository.getAllTransactions();
          vaultAccountLiveData = vaultRepository.getDefaultVault();
          transformations();
  
          final MutableLiveData&lt;Double&gt; resultLive = new MutableLiveData&lt;&gt;();
  
          try {
              result = transactionTotal.getValue() + accountAmount.getValue();
          } catch (NullPointerException e) {
              Toast.makeText(getApplication(), e.getMessage(), Toast.LENGTH_LONG).show();
              e.printStackTrace();
          }
          resultLive.postValue(result);
  
          totalAmount = Transformations.switchMap(resultLive, data -&gt; {
             final MutableLiveData&lt;String&gt; result = new MutableLiveData&lt;&gt;();
             String total = String.valueOf(data);
             result.postValue(total);
             return result;
          });
  
          return totalAmount;
      }
  
  
      private void transformations() {
  
          transactionTotal = Transformations.switchMap(transactionListLiveData, transactionsList -&gt; {
              final MutableLiveData&lt;Double&gt; result = new MutableLiveData&lt;&gt;();
              double amount = 0;
              for (int i = 0; i &lt; transactionsList.size(); i++) {
                  double itemAmount = Double.parseDouble(transactionsList.get(i).getTransactionAmount());
                  amount += itemAmount;
              }
              result.postValue(amount);
              return result;
          });
  
          accountAmount = Transformations.switchMap(vaultAccountLiveData, vault -&gt; {
              final MutableLiveData&lt;Double&gt; result = new MutableLiveData&lt;&gt;();
              result.postValue(Double.valueOf(vault.getInitAmount()));
              return result;
          });
  
      }
  
  }
  </code></pre>
  <p>The code did not work as I intended which shows that I have no idea what I'm doing.</p>
  <p>Nevertheless, I tried to test this code and found out the following:<br />
  1- the two repositories was returning null when I first call getFinalAmount and observe it in the view but if I cause change to the observed LiveData they return the live data I want to get.</p>
  <p>2- the switchMap inside transformations method was not working at all.</p>