Firebase addListenerForSingleValueEvent the debug mode works differently from the normal mode

61 views Asked by At

I have a calendar page in my project. When I run code, the addListenerForSingleValueEvent function gets random data. The data comes in a sequential manner when I run with Debug mode. Why does the normal mode work differently from the debug mode?

that firebase database;

"0588326969" : {
"196" : {
  "RUKIYE POLAT" : {
      "-LQJgZfDnhYG9bva1fV8" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "02/11/2018,16:52:24"
    },
    "-LQJg_D6BpNDWb1QpMrH" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "02/11/2018,16:52:25"
    },
    "-LQKOe-iAZntPmd5rTng" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "02/11/2018,20:09:22"
    },
    "-LQN65ZTecjRHRqr3Mt8" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "03/11/2018,8:47:10"
    },
    "-LQN66FbJ9B60gh0QasO" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "03/11/2018,8:47:11"
    },
    "-LQOl_D-YPkBorwd1ObT" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "03/11/2018,16:32:24"
    },
    "-LQSF9pnqcg9k6QFWJ9l" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "04/11/2018,8:44:53"
    },
    "-LQTrfXvBn_BXeKn7RBv" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "04/11/2018,16:17:09"
    },
    "-LQZ7kqtFLen3LxnVnAv" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "05/11/2018,16:49:51"
    },
    "-LQZgsTCEzpvyvzmBIzU" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "05/11/2018,19:27:41"
    },
    "-LQiRgVjqAnUamgPcNgj" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "07/11/2018,16:53:10"
    },
    "-LQiRh33jcpE2GLifTvz" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "07/11/2018,16:53:11"
    },
    "-LQj8tjeKtx0AJHt88G6" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "07/11/2018,20:10:40"
    },
    "-LQnaBSEUOxeBCUsO48y" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "08/11/2018,16:52:46"
    },
    "-LQnaBziiUf2NAURu2uF" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "08/11/2018,16:52:47"
    },
    "-LQoIpof4apMbNW2pzmS" : {
      "adSoyad" : "RUKIYE POLAT",
      "tarih" : "08/11/2018,20:12:11"
    }
  }
},
}

my code:

 Date date1=null;
    String bugununtarihi=new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
    SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
    try {
        date1 = sdf.parse(bugununtarihi);
    }
    catch (ParseException e){
        e.printStackTrace();
    }
    Calendar cal1=null;
    Calendar cal2=null;
    cal1=Calendar.getInstance();
    cal1.setTime(date1);
    cal2=Calendar.getInstance();
    cal2.setTime(date1);
    cal1.add(Calendar.DATE,-5);
    final ArrayList<HashMap<String,String>> veriler=new ArrayList<>();
    while (!cal1.after(cal2)){
         String birincitarih = sdf.format(cal1.getTime());
         cal1.add(Calendar.DATE, 1);
         DatabaseReference dbGelenler=db.getReference("0588326969"+"/"+"196"+"/"+"RUKIYE POLAT");
         Query query=dbGelenler.orderByChild("tarih").startAt(birincitarih).endAt(birincitarih+"\uf8ff");
         query.addListenerForSingleValueEvent(new ValueEventListener() {
             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                 List<String> isimler=new ArrayList<>();
                 for (DataSnapshot anahtar:dataSnapshot.getChildren()){
                     String isim=anahtar.getValue(ornekogrencigetir.class).getAdSoyad();
                     String tarih=anahtar.getValue(ornekogrencigetir.class).getTarih();
                     HashMap<String,String> user=new HashMap<>();
                          String[] seperated1;
                         seperated1 = tarih.split(",");
                         user.put("hashtarih", seperated1[0]);
                         user.put("hashsaat", seperated1[1]);
                         isimler.add(isim); veriler.add(user);
                     ListAdapter adapter = new SimpleAdapter(MainActivity.this, veriler, R.layout.takvimsatir,
                             new String[]{"hashtarih", "hashsaat"}, new int[]{R.id.takvimsatirtarih, R.id.takvimsatirsaat}) {
                     };
                     mainlist.setAdapter(adapter);
                 }
             }
             public void onCancelled(@NonNull DatabaseError databaseError) {
             }
         });
    }

When I work with debug mode, dates come in order.like 02,03,04,05,...

  When I run debug             when I run application 
mode output is:               output is:

04/11/2018  08:44:53         07/11/2018  16:53:10
04/11/2018  16:17:09         07/11/2018  16:53:11
05/11/2018  16:49:51         07/11/2018  20:10:40
05/11/2018  19:27:41         05/11/2018  16:49:51
07/11/2018  16:53:10         05/11/2018  19:27:41
07/11/2018  16:53:11         04/11/2018  08:44:53
07/11/2018  20:10:40         04/11/2018  16:17:09
08/11/2018  16:52:46         08/11/2018  16:52:46
08/11/2018  16:52:47         08/11/2018  16:52:47
08/11/2018  20:12:11         08/11/2018  20:12:11

NOTE:I use the same code in a different project.The dates in the project come in order,like values of debug mode. I want to get dates in order ,as above debug mode

Answer:I used setPersistenceEnabled(true); I created a class DataBaseUtil:

public class DataBaseUtil {
private static FirebaseDatabase mDatabase;

public static FirebaseDatabase getDatabase() {
    if (mDatabase == null) {
        mDatabase = FirebaseDatabase.getInstance();
        mDatabase.setPersistenceEnabled(true);
    }
    return mDatabase;
}}

I used this class db=DataBaseUtil.getDatabase() instead of db = FirebaseDatabase.getInstance() and I added private per FirebaseDatabase db,as that private FirebaseDatabase db;

0

There are 0 answers