title: How to use LiveData, when the DAO method requires a changing parameter?
body: <p>In a dictionary app using Room to store words I have a Fragment with a TextField.</p>
  
  <p>When the user enters a word into the TextField, I am extracting the text and send it to a JobIntentService, which then calls the DAO method synchronously on its own thread:</p>
  
  <pre><code>@Dao
  public interface MyDao {
      @Query("SELECT 1 FROM dictionary WHERE word = :word")
      int findWord(String word);
  }
  </code></pre>
  
  <p>Finally, the JobIntentService sends the result via LocalBroadcastManager to the hosting Activity and the activity calls a public method on the Fragment.</p>
  
  <p>This path works, but you can see that it is very long and I had to write lot of boilerplate code to implement it.</p>
  
  <p>Recently I've got a hint on Reddit, that LiveData is better for such cases and I have been reading on it and already switched <a href="https://stackoverflow.com/questions/53070525/fastadapter-flickers-after-loading-from-livedata-even-though-the-data-stays-same">some of my fragments</a> to using it.</p>
  
  <p>So I am trying to switch this Fragment to using LiveData too, by changing the signature of the method to:</p>
  
  <pre><code>@Dao
  public interface MyDao {
      @Query("SELECT 1 FROM dictionary WHERE word = :word")
      LiveData&lt;Integer&gt; findWord(String word);
  }
  </code></pre>
  
  <p>and invoking it from my Fragment:</p>
  
  <pre><code>public class MyFragment extends Fragment {
      private static final Pattern PATTERN = Pattern.compile("([A-Z]{2,})");
  
      private LiveData&lt;Integer&gt; mLiveData;
  
      private EditText mInputText;
      private CheckedTextView mResultText;
  
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater,
                               ViewGroup container,
                               Bundle savedInstanceState) {
          ...
          mInputText.addTextChangedListener(new TextWatcher() {
              ...
              @Override
              public void afterTextChanged(Editable s) {
                  String word = mInputText.getText().toString().trim().toUpperCase();
                  if (PATTERN.matcher(word).matches()) {
                      mLiveData = MyDatabase.getInstance(getContext()).myDao().findWord(word);
                  } else {
                      mResultText.setChecked(false);
                  }
              }
          });
  
          mLiveData.observe(this, found -&gt; {
              if (found != null &amp;&amp; found == 1) {
                  mResultText.setChecked(true);
              } else {
                  mResultText.setChecked(false);
              }
          });
  
          return view;
      }
  </code></pre>
  
  <p>And the above method of course fails with NPE because <code>mLiveData</code> is null.</p>
  
  <p>But how can I initiate it, when the word parameter to <code>findWord(word)</code> is changing?</p>