0

I'm new to Android and Firebase environment but I'm working on it !

I'm working on an Android app and I need to read some values related to a child within a Firebase database. After this initial read, I need to modify / update these values and write them to the same child.

public class MainActivity extends Activity {
    public  static  class  Shoe extends JSONObject  {
        private String  name;
        private int     size;
        Shoe(){      
               // Default constructor required for calls to 
               // DataSnapshot.getValue(Shoe.class)
        }
        Shoe( String nm, int sz)  { this.name = nm; this.size = sz;  }
        public int      getSize()          { return this.size; }
        public void     setSize(int sz)    { this.size = sz; }
        public String   getName()          { return this.name;}
        public void     setName(String nm) {this.name = nm; }
    }
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG, "onCreate");

            // Write a message to the database
            FirebaseDatabase    database      = FirebaseDatabase.getInstance();
            database.setPersistenceEnabled(true);

            DatabaseReference   myRefTarget   = database.getReference("target");

            Shoe obj1 = new Shoe("item ID 1", 99);
            Shoe obj2 = new Shoe("item ID 2", 1000);

            final Shoe obj_old = new Shoe();

            Shoe obj_new = new Shoe();

            DatabaseReference myRefDeviceA = myRefTarget.child("deviceA").getRef();
            myRefDeviceA.keepSynced(true);

            myRefDeviceA.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot)
                {
                    obj_old.setName( dataSnapshot.getValue(Shoe.class).getName());
                    obj_old.setSize( dataSnapshot.getValue(Shoe.class).getSize());

                    Log.d(TAG_CLOUD, "from onDataChange: deviceA = " + obj_old.getName() + ", " + obj_old.getSize());
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {   
                }
            });
           // HERE 
           Log.d(TAG_CLOUD, "Name = " + obj_old.getName() + ", Size = " + obj_old.getSize());
}

the issue I got is that the read operation is asynchronously done..

D/FROM CLOUD: Name = null, Size = 0
D/FROM CLOUD: from onDataChange: deviceA = item ID 1, 99

how can adapt / modify the source code in such way that first "read" to give me values different than null and '0' ? "HERE" line eg.

Name = item ID 1 Size = 99

Thank you.

1 Answers1

0

You don't suppose to perform networking operations on the UI thread. If you want to display the data in the activity, you should show a loading dialog in the onCreate method, and then after fetching the data close the dialog and update the activity view

regev avraham
  • 1,102
  • 1
  • 9
  • 24