Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet258/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   254   255   256   257   258   259   260   261   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

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!

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   254   255   256   257   258   259   260   261   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish