Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Implication of using AsyncTask to make repeated Ajax Calls
    primarykey
    data
    text
    <p>I need my Android app to periodically fetch data from a server using AJAX calls, and update the UI accordingly (just a bunch of <code>TextView</code>s that need to be updated with <code>setText()</code>). Note that this involves 2 tasks:</p> <ol> <li>Making an AJAX call, and updating the UI once I receive a response - I use a simple <code>AsyncTask</code> for this.</li> <li>Doing the above repeatedly, at regular intervals.</li> </ol> <p>I haven't figured out an elegant way to achieve Point 2 above. Currently, I am simply executing the task itself from <code>OnPostExecute()</code>. I read on <a href="https://stackoverflow.com/questions/6400810/android-asynctask-starting-another-asynctask">this thread at SO</a> that I need not worry about garbage collection as far as the AsyncTask objects are concerned.</p> <p>But I'm still unsure as to how I set up a timer that will fire my AsyncTask after it expires. Any pointers will be appreciated. Here is my code:</p> <pre><code>public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AjaxRequestTask().execute(MY_REST_API_URL); } private void updateReadings(String newReadings) { //Update the UI } class AjaxRequestTask extends AsyncTask&lt;String, Integer, String&gt; { @Override protected String doInBackground(String... restApiUrl) { //Do AJAX Request } @Override protected void onPostExecute(String result) { updateReadings(result); /*Is there a more elegant way to achieve this than create a new AsyncTask object every 10 seconds? Also, How can I update the UI if I create a timer here? */ new AjaxRequestTask().execute(MY_REST_API_URL); } } </code></pre> <p>}</p> <p>Thanks in advance</p> <p>EDIT: I tried posting an answer but couldn't do it since I don't have the reputation to answer within 8 hours.</p> <p>Well, so I found a solution. I'm not convinced however.</p> <pre><code>protected void onPostExecute(String result) { updateReadings(result); // super.onPostExecute(result); new Timer().schedule( new TimerTask() { @Override public void run() { new AjaxRequestTask().execute(MY_REST_API_URL); } }, TIMER_ONE_TIME_EXECUTION_DELAY ); } </code></pre> <ol> <li><p>Are there any flip sides that I should be aware of when I use this? In particular, I am seeing lots of GCs happening in the LogCat. Also, I am wondering how an <code>AsyncTask</code> can be candidate for GC unless the <code>onPostExecute()</code> completes?</p></li> <li><p>How can I "stop" the updates? One way I thought of was to make the very first <code>AsyncTask</code> instance as a member variable of the <code>Activity</code>. That way, I can invoke <code>cancel(true)</code> on it and hope that this will "stop" the tasks.</p></li> </ol> <hr> <h2><strong><em>SOLUTION</em></strong>:</h2> <p>In case anyone is looking for something similar - none of the solutions I mentioned here work satisfactorily. They all suffer from <code>OutOfMemory</code> issues. I did not debug into the details of the OOM, but I suspect it could either be because of the recursion, or because of having HTTP-related objects as member variables in the <code>AsyncTask</code> rather than as members of the <code>Activity</code> (basically because of NOT reusing HTTP and other objects).</p> <p>I discarded this approach for a different one - making my Ajax Calls endlessly in the <code>doInBackground()</code> of my <code>AsyncTask</code>; and updating the UI in <code>onProgressUpdate()</code>. That way I also avoid the overhead of maintaining too many threads or <code>Handler</code>s for updating the UI (remember UI can be updated in <code>onProgressUpdate()</code> ).</p> <p>This approach also eliminates the need for <code>Timer</code>s and <code>TimerTask</code>s, favoring the use of <code>Thread.sleep()</code> instead. <a href="https://stackoverflow.com/questions/6652390/android-timertask-scheduled-for-repetition-getting-fired-only-once">This thread on SO</a> has more details and a code snippet too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload