2
\$\begingroup\$

I have written a helper class to manage callbacks from Firebase's asynchronous calls. Can this be implemented in a better way?

public class FirebaseHelper {
    private static String TAG = FirebaseHelper.class.getSimpleName();

    private static FirebaseHelper mFirebaseHelper;
    private DatabaseReference mDatabase;
    private DataSnapshot cachedDataSnapshot = null;

    // Create a pool of 3 threads
    private final ExecutorService pool = Executors.newFixedThreadPool(3);

    /**
     * used to get object of FirebaseHelper class via singleton pattern
     * @return FirebaseHelper object
     */
    public static FirebaseHelper getInstance(){
        if(mFirebaseHelper == null){
            mFirebaseHelper = new FirebaseHelper();
        }
        return mFirebaseHelper;
    }

    /**
     * Used to register the root object of database from where data is to be accessed
     * @param databaseRoot
     */
    public void registerDatabaseRoot(String databaseRoot){
        mDatabase = FirebaseDatabase.getInstance().getReference(databaseRoot);
    }

    /**
     * get the current root
     * @return
     */
    public DatabaseReference getDatabaseRoot(){
        return mDatabase;
    }

    /**
     * get the closed lead disposition data
     * @return a dummy object until data is retrieved from firebase database
     */

    public Future<ArrayList<CLDP>> getLeadDisposition(final String Id){
        registerDatabaseRoot(FirebaseConstants.MASTER_DATA);

        return pool.submit(new Callable<ArrayList<CLDP>>() {

            @Override
            public ArrayList<CLDP> call() throws Exception {
                final Query query = mDatabase.child(FirebaseConstants.LEADS_DISPOSTION).
                        orderByChild("FLAG").equalTo("1");
                final ArrayList<CLDP> data = new ArrayList<>();

                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        cachedDataSnapshot = dataSnapshot;
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.e(TAG, "getCLDPId:onCancelled", databaseError.toException());

                    }
                });
                Iterable<DataSnapshot> snapshots = cachedDataSnapshot.getChildren();
                for (DataSnapshot d : snapshots){
                    if((d.child("TC_TypeId").getValue()).equals(Id)) {
                        CLDP cldData = new CLDP();
                        cldData.setId((String) d.child("TC_DispId").
                                getValue());
                        cldData.setName((String) d.child("Name").getValue());
                        data.add(cldData);
                    }
                }
                return data;
            }
        });
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.