What is the correct way to insert data into a database using AsyncTask

I am using AsyncTask to download data from a server, and I want to insert data into my database. Where should I put the embed code and how can I stop AsyncTask from inserting data if the user clicks the "Home" button?

I am currently using asynctask.cancel(true), but I cannot stop inserting the database.

public class TalkToServer extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // show progress dialog
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {
        //download data from the server
        return something;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // dismiss dialog
    }
}
+4
source share
3 answers

A better option would be to insert the database into onPostExecute().

If AsyncTaskcanceled, it onPostExecute()will not be called.

From the Documentation for AsyncTask.cancel () :

onCancelled (Object) doInBackground (Object []). , onPostExecute (Object) . , , isCancelled() doInBackground (Object []) .

isCancelled() doInBackground().

    @Override
    protected String doInBackground(String... params) {
        //download data from the server

        if (isCancelled()){

          //Prevent data from being inserted in the database

          try {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //Dismiss ProgressDialog
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

    return something;
    }

    @Override
        protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // dismiss dialog
        //Insert data into database
    }
+1

db doInBackground(String... params)

onPostExecute(String result) .

, , "" ( AsyncTask)

, ...

0
Create new class which contains data insertion function.
Ex - 

//create new class

public class DBInsert(){

    //write data insertion login in this function.
    public string insertData(){
      ......
      ......
    }
}

//your async task.
public class TalkToServer extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // show progress dialog 
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {
        //download data from the server

        //Call db insert function.
        DBInsert.insertData();

        return something;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // dismiss dialog
    }
}     
0
source

All Articles