lundi 13 juillet 2015

Check String array values exists in local database sqlite android

I have an Array with some string values that varies from 5 to 50 in size.

I have local table containing some data and now I want to check if each value of this array exists in the table or not.

I am using activeAndroid ORM for database requests.

Everything is working fine, except the fact that when this method gets called, application started getting laggy and really slow, sometimes even ANR crash

Here is my code which I am using...

 private void checkFor(final ArrayList<property> hash){
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            if(eDatabase == null)
                eDatabase = Database.getInstance();
            try {
                ActiveAndroid.beginTransaction();
                if (hash != null && hash.size() > 0) {
                    inbox = new ArrayList<>();

                    for (int i = 0; i < hash.size(); i++) {
                            if (eDatabase.Check(hash.get(i) == 0) {
                                inbox.add(hash.get(i));
                            }
                    }
                }
                ActiveAndroid.setTransactionSuccessful();
            } finally {
                ActiveAndroid.endTransaction();
            }

        }
    });
}

here is my Check method

 public int Check(String unique){
    List<Not> not = new ArrayList<>();
      try {
          ActiveAndroid.beginTransaction();
          not = new Select().from(Not.class)
                  .where("unique_id = ?", unique).execute();
          ActiveAndroid.setTransactionSuccessful();
      } finally {
          ActiveAndroid.endTransaction();
      }
    if(not != null && not.size() > 0) {
        return not.size();
    }
    else {
        return 0;
    }

any suggestion to optimised it, or doing it in better way I suspect that there quite alot database access queries eDatabase.Check(hash.get(i) which is making it slow...

Aucun commentaire:

Enregistrer un commentaire