Problem with Sqlite in Asynctask’s onPostExecute method

Asked

Viewed 96 times

0

I’m having a problem running an Sqlite function in the Asynctask onPostExecute method. When I implement the same function in Activity it runs normally, but in onPostExecute it doesn’t even enter the breakpoint I put in the function.

The function that performs the user update is the following:

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();
        }
    }
}

Queries class and 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 on onPostExecute:

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

I don’t know what it could be. I tried to put the function in a Thread and nothing. Any help is welcome :)

  • marked the onPostExecute method with @override?

  • I marked @Geferson, thanks for the answer. I solved the problem, I will answer my question to close.

1 answer

1


I solved the problem, it was not passing any value to the user at the moment of receiving this value in theInBackground. For understanding:

In the Asynctask call, I have now set the user value

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

So Asynctask now has user value:

protected JsonReturn doInBackground(JsonParameter... params){
    ...
    user = params[0].getUser();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.