Skip to content

Commit a37f703

Browse files
thatfiredevrlazo
authored andcommitted
docs: add docs for firebase-database-ktx (firebase#841)
* docs: add docs for firebase-database-ktx * docs: add database-ktx to README.md and FOSDC
1 parent 0a6cd09 commit a37f703

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

.opensource/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"docs/ktx/firestore.md": "Firestore KTX",
1212
"docs/ktx/functions.md": "Functions KTX",
1313
"docs/ktx/remote-config.md": "Remote Config KTX",
14-
"docs/ktx/storage.md": "Storage KTX"
14+
"docs/ktx/storage.md": "Storage KTX",
15+
"docs/ktx/database.md": "Realtime Database KTX"
1516
},
1617
"related": [
1718
"firebase/quickstart-android"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies:
88
* `firebase-common`
99
* `firebase-common-ktx`
1010
* `firebase-database`
11+
* `firebase-database-ktx`
1112
* `firebase-database-collection`
1213
* `firebase-datatransport`
1314
* `firebase-firestore`

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ in your app:
2828
* [`firebase-functions`](ktx/functions.md)
2929
* [`firebase-remote-config`](ktx/remote-config.md)
3030
* [`firebase-storage`](ktx/storage.md)
31+
* [`firebase-database`](ktx/database.md)
3132

3233
[android-setup]: https://firebase.google.com/docs/android/setup
3334
[main-readme]: https://github.com/firebase/firebase-android-sdk/blob/master/README.md

docs/ktx/database.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Realtime Database Kotlin Extensions
2+
3+
## Getting Started
4+
5+
To use the Firebase Realtime Database Android SDK with Kotlin Extensions, add the following
6+
to your app's `build.gradle` file:
7+
8+
```groovy
9+
// See maven.google.com for the latest versions
10+
// This library transitively includes the firebase-database library
11+
implementation 'com.google.firebase:firebase-database-ktx:$VERSION'
12+
```
13+
14+
## Features
15+
16+
### Get an instance of FirebaseDatabase
17+
18+
**Kotlin**
19+
```kotlin
20+
val database = FirebaseDatabase.getInstance()
21+
val anotherDatabase = FirebaseDatabase.getInstance(FirebaseApp.getInstance("myApp"))
22+
```
23+
24+
**Kotlin + KTX**
25+
```kotlin
26+
val database = Firebase.database
27+
val anotherDatabase = Firebase.database(Firebase.app("myApp"))
28+
```
29+
30+
### Get the FirebaseDatabase for the specified url
31+
32+
**Kotlin**
33+
```kotlin
34+
val database = FirebaseDatabase.getInstance(url)
35+
```
36+
37+
**Kotlin + KTX**
38+
```kotlin
39+
val database = Firebase.database(url)
40+
```
41+
42+
43+
### Get the FirebaseDatabase of the given FirebaseApp and url
44+
45+
**Kotlin**
46+
```kotlin
47+
val database = FirebaseDatabase.getInstance(app, url)
48+
```
49+
50+
**Kotlin + KTX**
51+
```kotlin
52+
val database = Firebase.database(app, url)
53+
```
54+
55+
### Convert a DataSnapshot to a POJO
56+
57+
**Kotlin**
58+
```kotlin
59+
val snapshot: DataSnapshot = ...
60+
val myObject = snapshot.getValue(MyClass::class.java)
61+
```
62+
63+
**Kotlin + KTX**
64+
```kotlin
65+
val snapshot: DocumentSnapshot = ...
66+
val myObject = snapshot.getValue<MyClass>()
67+
```
68+
69+
### Convert a DataSnapshot to generic types such as List or Map
70+
71+
**Kotlin**
72+
```kotlin
73+
val snapshot: DataSnapshot = ...
74+
val typeIndicator = object : GenericTypeIndicator<List<Message>>() {}
75+
val messages: List<Message> = snapshot.getValue(t)
76+
```
77+
78+
**Kotlin + KTX**
79+
```kotlin
80+
val snapshot: DocumentSnapshot = ...
81+
val messages: List<Message> = snapshot.getValue<List<@JvmSuppressWildcards Message>>()
82+
```

0 commit comments

Comments
 (0)