build.gradle
file and add the following dependency:
implementation 'com.google.firebase:firebase-firestore:
20.1
.
0
'
The second thing you need to do is to initialize the Firestore instance. Open the
CloudFirestoreManager
class and add
database
field:
private
val database =
FirebaseFirestore
.getInstance()
FirebaseFirestore
represents the Firestore instance and it is used for all the
communication with the Firestore database from the application.
Saving Data on Android
Chapter 17: Managing Data with Cloud Firestore
raywenderlich.com
252
Wri/ng data
The first part of the app that you'll refactor to use Firestore is writing. Instead of
writing data to Realtime Database you'll write the data to the Cloud Firestore.
In the
CloudFirestoreManager
class replace the
TODO
inside the
addPost
function
with the following:
val documentReference =
database.collection(
POSTS_COLLECTION
).document()
//1
val post =
HashMap
<
String
,
Any
>()
//2
//3
post[
AUTHOR_KEY
] = authenticationManager.getCurrentUser()
post[
CONTENT_KEY
] = content
post[
TIMESTAMP_KEY
] = getCurrentTime()
post[
ID_KEY
] = documentReference.id
//4
documentReference
.
set
(post)
.addOnSuccessListener { onSuccessAction() }
.addOnFailureListener { onFailureAction() }
1. First, you call the
collection
method passing in the posts collection path. The
collection
method returns a reference to the collection at the specified path in
the database. You use that collection reference to get the document reference by
calling the
document
method, which points to the new document within that
collection with an auto-generated ID. You'll use that document reference to get
the document ID that you'll store in the database with the post object.
2. Here, you create data that you want to save to the document. This data is
represented as a map of
String, Any
type where
String
is the type of the key
and
Any
is the type of the value.
3. You need to populate the map with the values that you want to write to the
database. You add author, post content, timestamp and the ID of the document to
the map.
4. This where you actually save the data. First, you call the
set
method on the
document reference that will replace the data in the document if it already exists
or it will create it if it doesn't. You pass in the
post
map that contains the data
that you want to write to that document. The
set
method returns a
Task
which
represent an asynchronous operation. Since the operation is asynchronous, you
attach two listeners,
OnSuccessListener
that is called if the
Task
completes
successfully, and
OnFailureListener
that is called if the
Task
fails. You pass in
Saving Data on Android
Chapter 17: Managing Data with Cloud Firestore
raywenderlich.com
253
the actions that you receive as the function parameters.
Open the
AddPostActivity
class and replace the
TODO
inside the
addPostIfNotEmpty
function with the call to the
cloudFirestoreManager.addPost
:
cloudFirestoreManager.addPost(postMessage, ::onPostAddSuccess, :
:onPostAddFailed)
Build and run your app. Click on the floating action button at the bottom right
corner. Add some text and click the
Post
button:
You should get a toast message that says that the post save is successful. Nothing is
displayed on the home screen. That is because you haven't implemented the logic for
reading yet.
Open the database in the console. You should see your post there. If you don't see it
try to refresh the page:
Saving Data on Android
Chapter 17: Managing Data with Cloud Firestore
raywenderlich.com
254
Congratulations, you saved your first post to the Firestore database!
Do'stlaringiz bilan baham: |