
Hi, today am going to share a quick summary which will show you how to put together your firebase Cloud Firestore, then you should be able to add data to it. The data we are going to add will be added programmatically within your app, and instantly the data should be visible in your firebase Cloud firestore console.
Getting Started
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
request.time < timestamp.date(2022, 10, 10);
}
}
}Do some maneuvers with your App
Again, you definitely should add your app to your firebase project. Having all that done, move to your app level build.gradle and add the following dependency
dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:30.3.1')
// Declare the dependency for the Cloud Firestore library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-firestore-ktx'
}
Create a Cloud firestore Instance in you Activity
The process should not be any different from any other instances you have encountered
val youNameIt = Firebase.firestore
Excuse my lack of formality, but my main purpose is just to remind you that the variable could be named any-how, just try and make the name relevant. We will use this instance to do a variety of stuff in the near future, just bare with me.
Data
I figured we need to round up on the same page about the data we are going to upload. You probably have a data class prepared for data entry, or some kind of handy hashMap maybe. Well, however you expect to have your data in place, we all meet at one point where we need a key-Value pair to successfully write data on the cloud firestore. Example UserName could be a key and any value can be placed there, Stanley say.
val dataToUpload = hashMapOf(
"UserName" to "Stanley",
"Email" to "projectsoftonia@gmail.com"
)
Add Data
Am gonna go ahead and remind you that the data we keep in Cloud firestore is normally stashed in one giant collection, though you could have a number of collections in the same database, say i have a collection of users and another for products, both of these collections have individual users in them, each user has to be differentiated from the other, this brings us to an obvious conclusion where we need a unique id for each member in a collection. Firebase automatically handles that for us in some way, or you could explicitly provide an id of your own,
youNameIt.collection("users")
.add(dataToUpload)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
Having done just that, you should be able to run your app and eventually find the data on the console. The data uploaded will be provided with an automatic document id. If you prefer to provide an id of your own, that's a very mature thing to do, but i should warn you, it's super risky. You could at some confuse the ids and cause a fatal conflict. An example below provides an explicit document id
// Add a new document with an explicit ID
youNameIt.collection("users").document("userOne")
.add(user)
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
Keep in mind, the user id you provide should never be redundant. And up to this point, our goal is achieved, we have hopefully succeeded to write data on your firebase Cloud firestore. You might wanna read your data from there, and that's what am going to write next. Leave me your comment on what you would like me to write more about.
Stay awesome,
#Sam.
0 Comments