Sample project to try out Firebase Database.
My main goal for creating this project was to understand how easy it would be set up a 'server' hosted by Firebase where I can easily send get
and post
requests with minimal setup. My only previous experience with this type of setup was with the Parse platform.
Steps I took to get things set up:
- Create a Firebase project in firebase.console.com
- Hook up my Firebase project to my Android project (setting it up through the console and adding the project's
google-services.json
file to my Android project) - In the Firebase console, create a database for my project.
- In the Firebase console, Add my first
collection
for my database. (I think of these as "endpoints".) My first collection wasposts
, which I envisioned as an endpoint similar to getting all posts for a social media feed. - Once a
collection
was created, I had to create the firstdocument
. A collection contains multiple documents, so I think of documents like individual JSON objects that return from a get() request. The firstdocument
looked something like{ {"description", "First!"}, {"user", "Caren"} }
. - Back in the Android app, Firebase is initialized like so:
val db = FirebaseFirestore.getInstance()
- Getting a post looks like:
// Similar to a 'get' request to the {posts} endpoint
db.collection("posts").get().addOnSuccessListener {
val posts = it.documents
for (post in posts) {
Log.i("Caren", "post " + post.get("description") + " " + post.get("timestamp"))
}
}
- Saving a post looks like:
val samplePost = hashMapOf(
"description" to "SECOND!",
"timestamp" to Calendar.getInstance().timeInMillis,
"user" to "Caren"
)
// Similar to a 'post' request to the {posts} endpoint
db.collection("posts").add(samplePost).addOnSuccessListener {
Log.i("Caren", "Added new post!")
}