Java Question Event get fired twice using AsyncTask

DonManfred

Expert
Licensed User
Longtime User
B4X:
    public void QueryASync(final String query){
        app.Log("lib:queryasync()");                               
        try {
            // run async
            new AsyncTask<Void, Void, Void>(){
                @Override
                protected void onPostExecute(Void result) {
                    //after async close progress dialog
                    //progressDialog.dismiss();
                   
                }

                @Override
                protected Void doInBackground(Void... params){
                    try {
                        final long startTime = System.currentTimeMillis();
                        // select a document and get bytes
                        List l = new anywheresoftware.b4a.objects.collections.List();
                        l.Initialize();

                      Statement st;
                        //app.Log("lib:queryasync():db.createStatement();");                               
                        st = db.createStatement();
                        //app.Log("lib:queryasync():ResultSet rs = st.executeQuery(query);");                               
                        ResultSet rs = st.executeQuery(query);
                        ResultSetMetaData rsmd = rs.getMetaData();
                       
                       
                        Map meta = new anywheresoftware.b4a.objects.collections.Map();
                        meta.Initialize();
                        meta.Put("ColumnCount", rsmd.getColumnCount());
                        //meta.Put("ErrorCode", db.getWarnings().getErrorCode());
                        //meta.Put("errorMessage", db.getWarnings().getMessage());
                        //meta.Put("SQLState", db.getWarnings().getSQLState());
                        int Recordcount = 0;
                        while(rs.next()) { 
                            //app.Log("lib:queryasync():ResultSetMetaData m = rs.getMetaData();");                               
                            Recordcount = Recordcount+1;
                            Map mm = new anywheresoftware.b4a.objects.collections.Map();
                            mm.Initialize();
                            int count = rsmd.getColumnCount();
                            for(int i=1; i <count+1; i++){
                                //app.Log("column: "+rs.getRow());                               
                        String column_name = rsmd.getColumnName(i);
                      if(rsmd.getColumnType(i)==java.sql.Types.ARRAY){
                                    mm.Put(column_name, rs.getArray(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.BIGINT){
                                    mm.Put(column_name, rs.getInt(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.BOOLEAN){
                                    mm.Put(column_name, rs.getBoolean(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.BLOB){
                                    mm.Put(column_name, rs.getBytes(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.DOUBLE){
                                    mm.Put(column_name, rs.getDouble(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.FLOAT){
                                    mm.Put(column_name, rs.getFloat(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.INTEGER){
                                    mm.Put(column_name, rs.getInt(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.NVARCHAR){
                                    mm.Put(column_name, rs.getNString(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.VARCHAR){
                                    mm.Put(column_name, rs.getString(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.TINYINT){
                                    mm.Put(column_name, rs.getInt(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.SMALLINT){
                                    mm.Put(column_name, rs.getInt(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.DATE){
                                    mm.Put(column_name, rs.getDate(i));                                   
                      } else if(rsmd.getColumnType(i)==java.sql.Types.TIMESTAMP){
                                    mm.Put(column_name, rs.getTimestamp(i));                                   
                      }else{
                                    mm.Put(column_name, rs.getObject(i));                                   
                      }
                                   
                            }
                            l.Add(mm);
                        }
                        meta.Put("RecordCount", Recordcount);                               
                      long stopTime = System.currentTimeMillis();
                      long elapsedTime = stopTime - startTime;
                        meta.Put("ms", elapsedTime);                               
                      //app.Log("lib:queryasync():time elapsed "+elapsedTime+"ms");                               
                         
                        if (app.subExists(eventName + "_queryresult")) {
                            app.Log("Raising.. "+eventName + "_queryresult()");                               
                            app.raiseEventFromDifferentThread(app.context, null, 0, eventName + "_queryresult", true, new Object[] {l,meta});
                        }else {
                            BA.Log("NOTFOUND '"+eventName + "_queryresult");
                        }
                        rs.close();
                        st.close();
                        //db.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        BA.Log("SqlException: "+e.toString()); 
                        e.printStackTrace();
                    } catch (Exception e){
                        app.Log("lib:Exception:"+e.toString());                               
                    }
                    return null;
                }
            }.execute();
            System.gc();// run GC
        }
        catch (Exception e) {
            app.Log("lib:error:"+e.toString());                               
        }
    }

It works but the event is raised two times. Why? Or what did i wrong? :)
 
Top