1
votes

I'm trying to save a document on a collection, following the documentation on cloud firestore. I can read a collection/document that I manually created on the firebase console but trying to store from my Android app is not working.

Console/Logcat is not showing any error and the app is not crashing. Neither of the listeners are getting called. Am I missing something?

class UserActivity : AppCompatActivity() {
    private val db = FirebaseFirestore.getInstance()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)
        save_button.setOnClickListener { storeUser() }
    }

    private fun storeUser(){
        val user = HashMap<String,Any>()
        user.put("first", "Ada")
        user.put("last", "Lovelace")
        user.put("born", 1815)

        db.collection("users")
                .add(user)
                .addOnSuccessListener(OnSuccessListener<DocumentReference> { documentReference -> Log.d("Storing User", "DocumentSnapshot added with ID: " + documentReference.id) })
                .addOnFailureListener(OnFailureListener { e -> Log.w("Storing User", "Error adding document", e) })
    }
}

EDIT:

As an update trying to read a document throws the following exception

com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline.

2
The success/failure listeners will only be called once the data is committed to, or rejected by, the server. Are you sure you have a connection to the server? - Frank van Puffelen
Do you have the right permissions to save data to the database? - Alex Mamo
@FrankvanPuffelen I think so, as i said on the question i can read data from the database. - Archer3CL
@AlexMamo I choose the "test" option when creating the database. This are the rules filled by default service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write; } } } - Archer3CL

2 Answers

1
votes

It seems that I needed to add the following permission to the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

But I find this weird because I could login using the firebase auth methods without it.

0
votes

I had a same problem. Kaspersky antivirus is guilty. Disable your antivirus. https://stackoverflow.com/a/50535344/3456124