Firebase中onDataChange()的奇怪HashMap错误

时间:2016-10-13 14:16:12

标签: android firebase hashmap firebase-realtime-database

我已经使用此功能来检查我的应用领域中的一些错误。 所以我已经制作了这段代码:

private int getSegnalations(final Location location, DatabaseReference ref, final HashMap<Location,String>mapLocation) {
     final List<Segnalation> matches = new LinkedList<>();
     final String key = mapLocation.get(location);
     Query query = ref.child("segnalation");

     segnalationDialog = createProgressDialog(getActivity(),segnalationDialog,getString(R.string.download));
     query.addValueEventListener(new ValueEventListener()
     {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot)
         {

          stopProgressDialog(segnalationDialog);

          for(DataSnapshot dS : dataSnapshot.getChildren())
          {
            Segnalation segnalation = dS.getValue(Segnalation.class);

            Log.d("keys:",segnalation.getIdPark()+","+mapLocation.get(location));

            if(segnalation.getIdPark().equals(key))
            {
              matches.add(segnalation);
            }
          }
         }

         @Override
         public void onCancelled(DatabaseError databaseError)
         {
          stopProgressDialog(segnalationDialog);
             showErrorMessage(getActivity(),getString(R.string.dbProblem));
         }
     });

     return matches.size();
 }

错误是Log.d向我展示了这一行:

D/keys:: -KTijPwCAd7M7wdZms3S,-KTy7n4WcCx3IUWRsRDX
D/keys:: -KTjJYQGW8_k6Dpi_z7W,-KTy7n4WcCx3IUWRsRDX
D/keys:: -KTjKby31PU7PkJWrb5U,-KTy7n4WcCx3IUWRsRDX
D/keys:: -KTy7n4WcCx3IUWRsRDX,-KTy7n4WcCx3IUWRsRDX

但是如果是旁路并且返回我的大小0而不是1。

为什么呢?这段代码中的错误是什么?

1 个答案:

答案 0 :(得分:1)

您的代码问题归因于synchronizationquery.addValueEventListener()启动新Thread以执行其操作。您的背景Thread会对其执行进行计数并使用return matches.size();结束该方法,该方法属于0因为此列表正在填充在其getSegnalations()方法结束后很久执行的其他线程中