Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 851
Sahdeep Singh Avatar asked Nov 12 '17 13:11

Sahdeep Singh


1 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)  }.

like image 80
Frank van Puffelen Avatar answered Nov 06 '22 11:11

Frank van Puffelen