Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data not being saved to Firebase from Android App

I have an existing application that I am trying to convert to use Firebase Database. Here is some code that I am using when I try to save an Object:

...
private FirebaseDatabase database;
private DatabaseReference waypointRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    database = FirebaseDatabase.getInstance();
    waypointRef = database.getReference(Utils.WAYPOINT_REFERENCE);

    waypointRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
            FBWaypoint wp = dataSnapshot.getValue(FBWaypoint.class);
            wp.setId(dataSnapshot.getKey());
            Log.e(TAG, String.format("Child Added:  %s, %s, %s, Prev: %s", wp.getName(), wp.getLatitude(), wp.getLongitude(), previousChildName));
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
            FBWaypoint wp = dataSnapshot.getValue(FBWaypoint.class);
            Log.e(TAG, String.format("Child Changed:  %s, %s, %s, Prev: %s", wp.getName(), wp.getLatitude(), wp.getLongitude(), previousChildName));
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            FBWaypoint wp = dataSnapshot.getValue(FBWaypoint.class);
            wp.setId(dataSnapshot.getKey());
            Log.e(TAG, String.format("Child Removed:  %s, %s, %s, Prev: %s", wp.getName(), wp.getLatitude(), wp.getLongitude()));
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
            FBWaypoint wp = dataSnapshot.getValue(FBWaypoint.class);
            Log.e(TAG, String.format("Child Moved:  %s, %s, %s, Prev: %s", wp.getName(), wp.getLatitude(), wp.getLongitude(), previousChildName));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, "Failed to add Child Listener.", databaseError.toException());
        }
    });
    ...
}

After creating an object, I save it like this:

waypointRef.push().setValue(wp);

I do see the callbacks firing in my logs at this point (along with values for the ids generated by firebase:

Child Added:  Hrbrbe, 44.969846, -89.670527, Prev: null
Child Added:  Hdbece, 44.956019, -89.666436, Prev: -KInIzqk6m96lwVWF6Pc
Child Added:  Hebdbr R, 44.970372, -89.673188, Prev: -KInJ17sfsQ4v_fUrAW3

However, I don't see anything in the firebase console for these records. Also when I restart the app, the onChildAdded callback does not get fired.

I also created another app, that just contains the code above...and that works fine. So I am not sure if something I am doing in my existing application is impacting firebase? However, I am not sure what that could be as I also took all of my dependancies and put them in the simple project as well.

Any ideas on what I can check next?

UPDATE: I added the onComplete Listener as recommended. It never gets called. If I add a FirebaseDatabase.purgeOutstandingWrites() after the setValue(), then the onComplete gets called with the following error:

DatabaseError: The write was canceled by the user.
like image 255
Innova Avatar asked May 27 '16 17:05

Innova


People also ask

How do I manually add data to Firebase?

If you open your app from Firebase dashboard, you can add data manually by clicking on the + sign. We will create a simple data structure.

How do I connect my Android app to Firebase?

Open the Firebase Assistant: Tools > Firebase. In the Assistant pane, choose a Firebase product to add to your app. Expand its section, then click the tutorial link (for example, Analytics > Log an Analytics event). Click Connect to Firebase to connect your Android project with Firebase.


1 Answers

Well I just figured it out. Thinking that there was a difference between my two projects was a red herring. The actual difference was between the two devices that I was deploying the two projects to. The working device was on a cellular connection. The device that wasn't working was on our corporate wifi network behind a proxy.

I do have the proxy set up correctly on the device, and I can access external sites...however, it looks like firebase does not use the proxy settings source.

Would be great if Firebase would use device proxy settings!

like image 116
Innova Avatar answered Oct 10 '22 23:10

Innova