Firestore operations (add(), set()) not working

Viewed 12966

I am trying to add user details to Firestore db, but can't write data in it.

Actually none of the listeners are triggered neither onSuccess() nor onFailure().

here is my code.

Map<String,Object> userlol = new HashMap<>();
userlol.put("name",name);
userlol.put("email",email);
userlol.put("uid",currentUser.getUid());
Log.d(TAG, "we are here");
CollectionReference dc = db.collection("users");
DocumentReference dd = dc.document(currentUser.getUid());
dd.set(userlol)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Toast.makeText(SignupActivity.this, "User Added" ,
                        Toast.LENGTH_SHORT).show();
                Log.d(TAG,"User added to database");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(SignupActivity.this, "ERROR" +e.toString(),
                        Toast.LENGTH_SHORT).show();
                Log.d(TAG, e.toString());
            }
        });

There is no toast nor the Log in my logcat.I can see

D/logging: we are here

This log and logs after this method.

There is no issue with rules as onFailure() is also not working.

I have searched every possible thing but nothing worked.

5 Answers

The only way I can get neither of the callbacks to trigger is when there is no network connection on my device. In that case the behavior makes sense, since the task is only considered completed when the data has been committed (or rejected) on the server.

To easily see if the Firestore client indeed can't connect to the server, enable debug logging:

FirebaseFirestore.setLoggingEnabled(true);

I see log entries like this after doing so:

11-12 07:56:21.366 10034-10066/com.firebase.firestorestackoverflow I/Firestore: (0.6.6-dev) [zzetk]: (b6322ac) Stream closed with status: zzcd{code=UNAVAILABLE, description=null, cause=java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname at java.net.InetAddress.lookupHostByName(InetAddress.java:470) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress.java:215) at io.grpc.internal.zzbj$zzb.zztu(Unknown Source) at io.grpc.internal.zzbk.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) at libcore.io.Posix.android_getaddrinfo(Native Method) at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55) at java.net.InetAddress.lookupHostByName(InetAddress.java:451) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)  at java.net.InetAddress.getAllByName(InetAddress.java:215)  at io.grpc.internal.zzbj$zzb.zztu(Unknown Source)  at io.grpc.internal.zzbk.run(Unknown Source)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  at java.lang.Thread.run(Thread.java:818)  }.

well for anyone having that problem in the future just Uninstall your application and reinstall it, as savage as it seams it works(y)

I had the same problem. First of all turning on the logs helped a lot as @Frank van Puffelen suggested. In my case I got "The Cloud Firestore API is not available for Datastore Mode projects". So I went to GCP and created manually a collection by changing the db to a native one. And then I had an option on the gui. Now the error changed to "FirebaseFirestoreException: permission_denied: missing or insufficient permissions" So I changed the permissions under the "rules" tab in firestore. And that fixed it for me :)

I guess the problem was the permissions from the beginning, but I can't tell for sure now.

I had the same error. In my case, I was creating a user and automatically logging him/her out, for them to log manually. I removed the sign out, as data appears not to be written if there is no user logged in. Do you have a user signed in when the data is written? Creating a user signs him/her in automatically. Hope it helps!

It appears that just like after adding new permissions in the manifest file, the app has to be reinstalled to register the changes. I tried reinstalling the app after adding the firestore connection and everything worked fine. voila!

Related