|
| 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