Skip to content

Commit e8d8250

Browse files
committed
espresso published and cloud firestore updated
1 parent e93ec4d commit e8d8250

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

_drafts/2019-04-08-cloud_firestore.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ image: firebase/cloud_firestore
77
github: firebase/tree/master/cloud_firestore
88
description: "Firebase"
99
version: Firebase-Firestore 18.0
10-
keywords: "firebase, chmura, dane, baza, cloud, storage, database, nosql, android, programowanie, programming"
10+
keywords: "firebase, chmura, dane, baza, cloud, storage, database, nosql, dokument, kolekcja, referencja, zapis, odczyt, transakcje, document, collection, reference, save, set, get, add, delete, update, android, programowanie, programming"
1111
---
1212

1313
## Wprowadzenie
@@ -16,7 +16,7 @@ keywords: "firebase, chmura, dane, baza, cloud, storage, database, nosql, androi
1616
## Model
1717
Dane reprezentowane są w postaci dokumentów (zbliżonych w stukurze do formatu `JSON`) przechowywanych w zorganizowanej kolekcji w hierarchicznej strukturze bazy danych `NoSQL` (w przeciwieństwie do `SQL` nie ma tabel ani wierszy). Każdy `dokument` (`document`) składa się z nazwy, zbioru par `klucz - wartość` (prymityw lub obiekt złożony) i może posiadać obiekty zagnieżdzone i podkolekcje. `Kolekcje` (`collection`) pełnią rolę kontenera dla różnych dokumentów i nie mogą posiadać innych kolekcji, a w doborze ich zawartości warto zachować logiczny porządek dzieląc dokumenty ze względu na kategorie i przeznaczenie co upraszcza poruszanie się po strukturze w kodzie klienta. `Podkolekcja` jest kolekcją w dokumencie i służy do budowania zagnieżdzonej struktury folderów.
1818

19-
//TODO rysunek
19+
![Model bazy](/assets/img/diagrams/firebase/database.png){: .center-image }
2020

2121
Ze względu na optymalizacje wydajności dostępu do bazy danych wprowadzony został mechanizm indeksowania, który wyróżnia dwa rodzaje indeksów: `single-field` (przechowuje posortowaną mapę wszystkich dokumentów zawierających dane pole) i `composite` (przechowuje posortowaną mapę wszystkich dokumentów zawierających wiele danych pól). Cloud Firestore jest zaprojektowany przede wszystkim z myślą o dużych kolekcjach małych dokumentów. Aby uzyskać dostęp do dokumentu lub kolekcji należy uzyskać `referencje` - obiekt typu `DocumentReference`.
2222

@@ -111,8 +111,7 @@ private fun updateData() {
111111

112112
private fun updateArrayData() {
113113
//add new data
114-
database.collection("club").document("acmilan").set(club, SetOptions.merge())
115-
114+
val reference = database.collection("club").document("acmilan")
116115
reference.update("uclTrophiesYear", FieldValue.arrayUnion(2007))
117116
//add some listeners
118117
}
@@ -182,28 +181,32 @@ private fun getDocumentsByQueries() {
182181
Ponadto istnieje możliwość nasłuchiwania modyfikacji danych dla dokumentu i kolekcji w czasie rzeczywistym poprzez dodanie obiektu słuchacza z uwzględnieniem źródła i typu zmian.
183182

184183
{% highlight kotlin %}
185-
private fun listenForDocumentChanges() {
186-
val reference = database.collection("club").document("acmilan")
187-
188-
reference.addSnapshotListener(EventListener<DocumentSnapshot> { snapshot, exception ->
184+
private fun listenForChanges() {
185+
val documentRef = database.collection("club").document("acmilan")
186+
documentRef.addSnapshotListener(EventListener<DocumentSnapshot> { snapshot, exception ->
189187
//check is exception
190188
if (exception != null) return@EventListener
191189

192190
//check source of the changes
193191
if (snapshot != null && snapshot.metadata.hasPendingWrites()) {
194192
//local changes
195193
}
196-
else {
194+
else {
197195
//server changes
198196
}
199197

198+
//do more job
199+
})
200+
201+
val collectionRef = database.collection("club")
202+
collectionRef.addSnapshotListener(EventListener<QuerySnapshot> { snapshots, exception ->
200203
//check type of the change
201204
for (changes in snapshots!!.documentChanges) {
202205
//could be: DocumentChange.Type.ADDED, MODIFIED or REMOVED
203206
}
204-
})
205207

206-
//do the same on the multiple documents from collection to listen more documents
208+
//do more job
209+
})
207210
}
208211
{% endhighlight %}
209212

@@ -217,7 +220,7 @@ private fun makeTransaction() {
217220
database.runTransaction { transaction ->
218221
val snapshot = transaction.get(reference)
219222
val budget = snapshot.getLong("budget")
220-
if(budget <= 1000)
223+
if(budget!! <= 1000)
221224
transaction.update(reference, "budget", budget + 500L)
222225
}
223226
}
@@ -231,13 +234,13 @@ private fun makeWriteBatch() {
231234
batch.set(rmReference, realMadrid)
232235

233236
//update current
234-
val acmReference = databae.collection("club").document("acmilan")
237+
val acmReference = database.collection("club").document("acmilan")
235238
batch.update(acmReference, "budget", 2000L)
236239

237240
//do more add, update, delete operations
238241

239242
//commit operations and add some listeners
240-
batch.commit.addOnCompleteListener {
243+
batch.commit().addOnCompleteListener {
241244
//some action
242245
}
243246
}

_posts/kotlin/2018-12-10-klasy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ var product1 = Product("Jack Daniels")
199199
product1.price = 10f //note that price isn't a part of autogenereated equals, hashCode, toString and copy methods
200200
print(product1.toString()) //Product(name=Jack Daniels)
201201
var product2 = product1.copy()
202-
print(product1.equals(product2)) //true - despite the differenet prices
202+
print(product1.equals(product2)) //true - despite the different prices
203203
{% endhighlight %}
204204

205205
## Klasy zagdnieżdzone i wewnętrzne
File renamed without changes.
37.9 KB
Loading

0 commit comments

Comments
 (0)