0

I am a core Java guy and a bit new to android. I want to follow the same old class design pattern what I do in my Java application:

Considering a standard crud. A Base DAO which will create on single instance of Database (FirebaseDatabase):

FirebaseDatabase database = FirebaseDatabase.getInstance();

A EntityDAO extends BaseDAO uses the database from BaseDAO and gets reference says Product:

DatabaseReference productRef = database.getRef("products");
productRef.addValueEventListener(...)

There will be a service class which calls the DAO to access the product data.

How do I modify this pattern to handle the async onDataChanged?

Sample Code:

//ProductDAO.java

 public class ProductDAO {
    private FirebaseDatabase fb;
    private DatabaseReference productRef;
    private Product product;
       public ProductDAO(){    
         fb  = FirebaseDatabase.getInstance(); //Already linked in the firebase console
         productRef = fb.getDatabaseReference("product");
         productRef.addValueEventListener( new ValueEventListener(){
         onDataChange(..){
                Log.i("AZ", dataSnapshot.getValue().toString());
       }   

      });

      }

    public ProductDAO getInstance(){
      return new ProductDAO();
    }

    public Product getProduct(){
        return Product; //This will be null because the listener is not     completed when the getInstance loads;
    }
 }

//ProductService.java

public class ProductService{
  private ProductDAO productDAO = ProductDAO.getInstance();

   public Product getProducts(){
      return productDAO.getProducts(); //This will always return null
   }
}
madhairsilence
  • 3,787
  • 2
  • 35
  • 76
  • I'm having a hard time understanding your question, since it is mostly about a model that you seem to know well, but I don't. Can you share the [minimal code that reproduces the problem](http://stackoverflow.com/help/mcve)? – Frank van Puffelen Dec 05 '16 at 06:00
  • But if you're looking to abstract away the fact that data is loaded asynchronously, you may want to read my answer here: http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Dec 05 '16 at 06:00
  • @FrankvanPuffelen .Thanks for your comment. Blatantly am trying to make a MVC kind of design here. But the only problem here is , unlike out traditional database query to retrive a list of db items, firebase always works async. When I say getInstance() of DAO. The dao is actually not ready. and it would always return null value. – madhairsilence Dec 05 '16 at 06:09
  • Added some more code – madhairsilence Dec 05 '16 at 06:09

1 Answers1

0

Trying to shoehorn an asynchronous model in to a synchronous model is a recipe for future pain.

Firebase doesn't just get data, it synchronizes changes between the server and the app. It is what is known as a reactive programming model: your app should be ready to display the data when it changes.

In Firebase's model, there is little difference between the data being available initially or it changing later on. Either of those triggers the method(s) in a data listener that you've attached.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Completely agree with your point and even I understand. My question just to know what would be the solution. I dont want to add onDataChange in every other class and pass my object every time to reach the other. Do we have any pattern to follow – madhairsilence Dec 05 '16 at 07:13
  • The pattern is to follow the Firebase documentation and examples and implement listeners. Not wanting to do so is not an option. – Frank van Puffelen Dec 05 '16 at 09:16