Android Firebase wait for data

ぃ、小莉子 提交于 2020-01-04 05:44:09

问题


in my android application I create an activity which contains a ListView which is populated with data from Firebase Database.

The JSON Tree of the structure of the database is the following:

{
  "companies" : {
    "companyX" : {
      "address" : "50th avenue, NY",
      "name" : "Spare-Tools Ltd."
    },
    "companyZ" : {
      "address" : "50th Broadway, NY",
      "name" : "Burgers and Burgers"
    }
  },
  "company-requests" : {
    "companyX" : {
      "req1" : true
      "req2" : true
    }
  },
  "requests" : {
    "req1" : {
      "destination" : "Upper Tooting 122, Bronx",
      "origin" : "Philadelphia",
      "time" : "1473593287",
      ...
    }
    "req2" : {
      ...
    }
  }
}

I want to populate the ListView with the list of requests from the requests node. But I first need to know all requests that belong to a specific company so I first go to the company-requests node and retrieve all the request-keys belonging to the specific company. The problem I am facing is that the ListView is created before the final data from the database arrived:

public class RequestsListActivity extends AppCompatActivity {
    private ListView rListView;
    DatabaseReference rootNode = FirebaseDatabase.getInstance().getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        rListView = (ListView) findViewById(R.id.result_list_view);
        //First I retrieve all the requests of a specific company
        DatabaseReference companyRequests = rootNode.child("company-requests/companyX");

        companyRequests.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Then I retrieve all the keys of these requests
                ...
                while (iterator.hasNext()) {
                    String key = iterator.next().getKey();
                    //For each key I retrieve its details from the requests node
                    DatabaseReference currRequest = rootNode.child("requests/" + key);
                    currRequest.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            String time;
                            time = (String) dataSnapshot.child("time").getValue();
                            Request request = new Request(time);
                            allRequests.add(request);
                        }
                        ...onCancelled...
                    });
                }
                //THIS CODE IS EXECUTED TO EARLY: BEFORE WE HAVE ANY DATA FROM FIREBASE
                RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this, allRequests);
                rListView.setAdapter(adapter);

                }

            ...onCancelled...
        });
    }
}

How can I insert a wait (spinner?) that waits until the values are loaded from Firebase?


回答1:


You can use a simple counter to keep track of the number of pending loads:

companyRequests.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        // at the start we need to still load all children
        final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            //For each key I retrieve its details from the requests node
            DatabaseReference currRequest = rootNode.child("requests/" + childSnapshot.getKey());
            currRequest.addListenerForSingleValueEvent(new ValueEventListener() {
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String time;
                    time = (String) dataSnapshot.child("time").getValue();
                    Request request = new Request(time);
                    allRequests.add(request);

                    // we loaded a child, check if we're done
                    pendingLoadCount[0] = pendingLoadCount[0] - 1;
                    if (pendingLoadCount[0] == 0) {
                        RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this, allRequests);
                        rListView.setAdapter(adapter);
                    }
                }
                ...onCancelled...
            });
        }
    }
});



回答2:


I solved this using a java.util.concurrent.CountDownLatch:

In this example, replace EquityTotalListener with your implementation of ValueEventListener.

private void recalculate() {
        final AtomicLong sumUpAll = new AtomicLong();
        final CountDownLatch cnt = new CountDownLatch(mapUid2GeoLocation.keySet().size());
        for (final String uid : mapUid2GeoLocation.keySet()) {
            EquityTotalListener el = mapUid2EquityListener.get(uid);
            if (el != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Listener for " + uid + " already set up");
                    cnt.countDown();
                }
            } else {
                el = new EquityTotalListener(database.getDatabase(), uid) {

                    @Override
                    public void onCancelled(final DatabaseError databaseError) {
                        super.onCancelled(databaseError);
                        cnt.countDown();
                    }

                    @Override
                    protected void valueChanged(final String key, final Object value) {
                        if (value != null) {
                            sumUpAll.getAndAdd(Long.parseLong(value.toString()));
                            cnt.countDown();
                        }
                    };
                }.attach();
                mapUid2EquityListener.put(uid, el);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Waitung for countdown..");
        }
        try {
            final boolean allGood = cnt.await(10, TimeUnit.SECONDS);
            if (allGood) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Done waiting, " + uid + " owns " + sumUpAll.get() + " equity");
                }
            } else {
                if (logger.isWarnEnabled()) {
                    logger.warn("Waiting for read operations ran into timeout");
                }
            }
        } catch (final InterruptedException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getLocalizedMessage(), e);
            }
        }

    }


来源:https://stackoverflow.com/questions/39477847/android-firebase-wait-for-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!