title: How to access and return the result of an AsyncTask from a repository?
body: <p>I'm new to coding, put already in a lot of hours and feel like I'm progressing step by step. However, I've encountered a problem that I was not able to solve even after days of researching and trying out different things. Sorry for being wordy or bad explanations - I'm trying to explain it as understandable as possible.</p>
  <p>Thank you so much in advance already!</p>
  <p>I'll describe the problem and what I have tried first. Then, down below you can see some more information on my final goal and why I thought about building the code the way it is. In the end you can find my code that I have so far.</p>
  <p><strong>Problem and potential solutions</strong></p>
  <p>The app I'm trying to code uses MVVM structure - I use Android Room and have a (pre-populated) database, two entities and corresponding DAOs, a view model and a repository.</p>
  <p>In my repository I am trying to include a new method &quot;getEntriesCountById&quot; that takes an Integer (&quot;correspondingChallengeId&quot;) and passes it to a new AsyncTask. Given that id, the AsyncTasks runs the corresponding query within the EntryDao to retrieve another integer that serves as the &quot;entriesCount&quot;.</p>
  <p>However, I cannot find a way to return the result (the &quot;entriesCount&quot;) of that asyncTask within my repository-method &quot;getEntriesCountById&quot;. I assume that I need to return it, so that I can call that method from another activity or the viewModel.</p>
  <p>I tried to wrap the return variable in LiveData and I tried to make use of the PostExecute Method, but yet couldn't find a way to make it work. I also looked into the usage of Weak References and the &quot;delegate&quot; function but somehow I just cannot return that &quot;entriesCount&quot;. By the way - I was successful in getting and showing the list of all entries (in a recycler view). I feel like it really is just as small step that I am missing.
  (By the way the repository in the code below includes the operation &quot;return list of all available entries and the corresponding asyncTask - This is I guess irrelevant for this question.)</p>
  <p><strong>Background information</strong></p>
  <p>To be more precise, the described entity is called &quot;ProgressEntry&quot; which is basically serving as a table to log someone's progress for different challenges (running, swimming, playing the guitar and so on). It includes a (primary, unique) entryId, a correspondingChallengeId, an entryDate and an entryValue. One record in the table could be: e.g.: 1, 3, 09/10/2020, 5000) meaning the record has the unique id &quot;1&quot;, the correspondingChallengeId &quot;3&quot; (that identifies the corresponding challenge, e.g. running), the particular date and the value of e.g. 5000 meters. Obviously, there can be many entries for the same correspondingChallengeId (e.g. if a person runs 5 times during September).</p>
  <p>In the repository I want to use Async Tasks to handle various DAO SQL Queries such as &quot;return a list of progress entries with correspondingChallengeId = 1, or &quot;return the entry with the highest value&quot; or &quot;return number of entries for a particular challenge&quot; (as in my case stated above).</p>
  <p><strong>Code snippets</strong></p>
  <p><strong>My ProgressEntry Class</strong></p>
  <pre><code>@Entity(tableName = &quot;progress_entries_table&quot;)
  public class ProgressEntry {
  
  @PrimaryKey (autoGenerate = true)
  private int progressEntryPrimaryId;
  //gives information about correspondingChallenge
  private int correspondingChallengeId;
  //date for which entry was recorded
  private String progressEntryDate;
  //progress value for particular entry
  private int progressEntryValue;
  
  public ProgressEntry(int correspondingChallengeId, String progressEntryDate, int progressEntryValue) {
      this.correspondingChallengeId = correspondingChallengeId;
      this.progressEntryDate = progressEntryDate;
      this.progressEntryValue = progressEntryValue;
  } 
  </code></pre>
  <p>(includes setters and getters for all fields/columns)</p>
  <p><strong>My ProgressEntry Dao (Query)</strong></p>
  <pre><code>//return number of entries for given id (representing unique challenge)
  @Query(&quot;SELECT count(*) FROM progress_entries_table WHERE correspondingChallengeId =:id&quot;)
  Integer getEntriesCountById(int id);
  </code></pre>
  <p><strong>My Repository</strong></p>
  <p>Note: the AsyncTask I was describing is at the top/ in the middle. The method &quot;getAllAvailableProgressEntries&quot; at the bottom is irrelevant and actually works in my application&quot; quite well. I included it because it could maybe give a hint on what I am doing wrong.</p>
  <pre><code>public class Repository {
  private ProgressEntryDao progressEntryDao;
  private LiveData&lt;List&lt;ProgressEntry&gt;&gt; allAvailableProgressEntries;
  
  public Repository(Application application) {
      Database database = Database.getInstance(application);
  
      progressEntryDao = database.progressEntryDao();
      allAvailableProgressEntries = progressEntryDao.getAllAvailableProgressEntries();
  }
  
  //--------------------------------------------------------
  
  public Integer getEntriesCountById(Integer correspondingChallengeId) {
      new CountAsyncTask(progressEntryDao).execute(correspondingChallengeId);
      //return... - How do I return the Integer (entriesCount)
  }
  
  //passing and Integer (correspondingChallengeId) and result is also supposed to be an Integer (entriesCount)
  private static class CountAsyncTask extends AsyncTask&lt;Integer, Void, Integer&gt;{
      private ProgressEntryDao progressEntryDao;
  
      //Constructor for AsyncTask
      public CountAsyncTask(ProgressEntryDao progressEntryDao) {
          this.progressEntryDao = progressEntryDao;
      }
  
      //doInBackground method accessing my Dao and the required method
      @Override
      protected Integer doInBackground(Integer... integers) {
          return progressEntryDao.getEntriesCountById(integers[0]);
      }
  
      //onPostExecute method - It should handle the result, right?
      @Override
      protected void onPostExecute(Integer integer) {
          super.onPostExecute(integer);
      }
  
  }
  
  //--------------------------------------------------------
  public LiveData&lt;List&lt;ProgressEntry&gt;&gt; getAllAvailableProgressEntries() {
      return allAvailableProgressEntries;
  }
  
  public void insertProgressEntry(ProgressEntry progressEntry) {
      new InsertProgressEntryAsyncTask(progressEntryDao).execute(progressEntry);
  }
  
  private static class InsertProgressEntryAsyncTask extends AsyncTask&lt;ProgressEntry, Void, Void&gt; {
      private ProgressEntryDao progressEntryDao;
  
      //Constructor
      private InsertProgressEntryAsyncTask(ProgressEntryDao progressEntryDao){
          this.progressEntryDao = progressEntryDao;
      }
  
      @Override
      protected Void doInBackground(ProgressEntry... progressEntries) {
          progressEntryDao.insert(progressEntries[0]);
          return null;
      }
  }
  </code></pre>
  <hr />
  <p><strong>How I am calling the method from an other activity</strong>
  (Doing it without view model for reasons of simplicity.)</p>
  <pre><code>public class ExperimentActivity extends AppCompatActivity {
  private TextView textView;
  private Repository repository;
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_experiment);
  
      repository = new Repository(getApplication());
  
      textView = findViewById(R.id.txtView);
  
  
      textView.setText(String.valueOf(repository.getEntriesCountById(2)));
  </code></pre>
  <hr />