2

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.

4
  • 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? Commented Aug 1, 2018 at 1:58
  • Do you have the right permissions to save data to the database? Commented Aug 1, 2018 at 9:05
  • @FrankvanPuffelen I think so, as i said on the question i can read data from the database. Commented Aug 1, 2018 at 15:41
  • @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; } } } Commented Aug 1, 2018 at 15:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.