Problem with SQLite in the onPostExecute method of AsyncTask

0

I'm having a problem running an SQLite function in the onPostExecute method of AsyncTask. When implementing the same function in the Activity it executes normally, but onPostExecute does not even enter the breakpoint I put in the function.

The function that performs the user update is as follows:

public class UpdateDatabaseOff{
    ...
    public static void updateUser(Context context, JSONObject object){
        try{
            User user;
            user = new User(object);
            Queries.setDataToDatabase(context, User.tableName, user);
        }
        catch(JSONException e){
            e.printStackTrace();
        }
    }
}

The Queries class and the setDataToDatabase function are below:

public static Integer setDataToDatabase(Context context, String tableName, GenericsTable genericsTable){
    long id = -1;

    getDatabase(context).get().beginTransaction();
    try {
        if (genericsTable.getSingleCode() != null) {
            int rowsUpdate = getDatabase(context).get().update(tableName, genericsTable.getContentValues(), genericsTable.getSingleCode(), null);
            if (rowsUpdate == 0) {
                id = getBanco(context).get().insert(tableName, null, genericsTable.getContentValues());
            }else{
                StringBuffer query = new StringBuffer();
                query.append(" SELECT id FROM " + tableName);
                query.append(" WHERE " + genericsTable.getSingleCode() );

                final Cursor cursor = Queries.getSelect(context, query);
                id = cursor.getLong(cursor.getColumnIndex("id"));
            }
        }else{
            id = getBanco(context).get().insert(tableName, null, genericsTable.getContentValues());
        }
        getBanco(context).get().setTransactionSuccessful();
    } finally {
        getBanco(context).get().endTransaction();
    }

    return new Integer(String.valueOf(id));
}

Then, finally, I call the updateUser function in onPostExecute:

protected void onPostExecute(JsonReturn jsonReturn){
    ...
    UpdateDatabaseOff.updateUser(context, jsonenvio); //Não funciona
}

I do not know what it can be. I tried putting the function on a Thread and nothing. Any help is welcome:)

    
asked by anonymous 06.04.2016 / 16:03

1 answer

1

I solved the problem, was not passing any value to user when receiving this value in doInBackground. For understanding:

In the AsyncTask call, I now set the user value

JsonParameter jsonParameter = new JsonParameter();
jsonParameter.setUser(user);
...
ConnectTask connectTask = new ConnectTask(context);
connectTask.execute(jsonParameter);

Then in AsyncTask it now has the value of the user:

protected JsonReturn doInBackground(JsonParameter... params){
    ...
    user = params[0].getUser();
}
    
09.04.2016 / 12:17