Setting the file. One moment.
Chapter 07 · Firebase Data Connect
Subchapter 7.9
reference/sdk_android.mdMarkdown4 KBView on GitHub
Consult this file when writing Android application code (Kotlin) that interacts with the SQL Connect backend.
EnumValue. You must unwrap it into
EnumValue.Known or EnumValue.Unknown to handle schema updates gracefully.execute() method..execute() within a coroutine scope for
asynchronous operations.Ensure you have the Kotlin Serialization plugin and standard SQL Connect dependencies:
plugins {
kotlin("plugin.serialization") version "1.8.22" // Must match Kotlin version
}
dependencies {
// [AGENT] Fetch the latest available BoM version from https://firebase.google.com/support/release-notes/android before adding this
implementation(platform("com.google.firebase:firebase-bom:34.12.0"))
implementation("com.google.firebase:firebase-dataconnect")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
}Retrieve the generated connector instance:
import com.google.firebase.dataconnect.generated.MoviesConnector
val connector = MoviesConnector.instance
// For local development with emulator
// Defaults to correct host for Android emulator (10.0.2.2)
connector.dataConnect.useEmulator()
// Or specify a non-default port:
// connector.dataConnect.useEmulator(port = 9999)val result = connector.listMovies.execute()
result.data.movies.forEach { movie ->
println(movie.title)
}val newMovie = connector.createMovie.execute(
title = "Empire Strikes Back",
releaseYear = 1980,
genre = "Sci-Fi",
rating = 5
)Unwrap the EnumValue to handle known and unknown cases safely.
val result = connector.listMovies.execute()
result.data.movies.forEach { movie ->
when (val aspect = movie.aspectratio) {
is EnumValue.Known -> println("Known aspect: ${aspect.value.name}")
is EnumValue.Unknown -> println("Unknown aspect: ${aspect.stringValue}")
}
}Enable caching in connector.yaml to reduce requests and support offline
scenarios.
generate:
kotlinSdk:
outputDir: "../android"
package: "com.google.firebase.dataconnect.generated"
clientCache:
maxAge: 5s
storage: persistent # Default for Android is persistentUse policies in code:
val queryResult = queryRef.execute(QueryRef.FetchPolicy.CACHE_ONLY)
val queryResult = queryRef.execute(QueryRef.FetchPolicy.SERVER_ONLY)String -> Kotlin StringInt -> Kotlin Int (32-bit)Float -> Kotlin Double (64-bit)Boolean -> Kotlin BooleanUUID -> Kotlin java.util.UUIDDate -> Kotlin com.google.firebase.dataconnect.LocalDateTimestamp -> Kotlin com.google.firebase.TimestampInt64 -> Kotlin LongAny -> Kotlin com.google.firebase.dataconnect.AnyValue