name: portada class: portada-slide, center, middle # Realm - Local .footnote[Mateu Yábar Valles] --- --- # Realm >> Realm is a fast, scalable alternative to SQLite with mobile to cloud data sync that makes building real-time, reactive mobile apps easy. --- # Realm - Base de dades nosql per aplicacions - Permet la sincronització amb Atlas MongoDB --- # Gradle ``` plugins { (...) id("io.realm.kotlin") version "1.5.0" } ``` ``` implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") implementation("io.realm.kotlin:library-sync:1.5.0") ``` - Assegurat usar Kotlin 1.7.21 --- # Model ``` open class Item( @PrimaryKey var _id: ObjectId = ObjectId.create(), var complete: Boolean = false, var summary: String = "", var owner_id: String = "" ) : RealmObject { // Declaring empty contructor constructor() : this(owner_id = "") {} //var doAfter: RealmList
? = realmListOf() override fun toString() = "Item($_id, $summary)" } ``` --- # Configuració només local ``` val config = RealmConfiguration.Builder(setOf(Item::class)) .deleteRealmIfMigrationNeeded() // .directory("customPath") .build() println("Realm Path: ${config.path}") val realm = Realm.open(config) ``` --- # Insertar ``` realm.writeBlocking { val item = Item(summary = "Some summary ${Date()}" // doAfter = query
().find().take(2).toRealmList() ) copyToRealm(item) } ``` --- # Consultar ``` val items = realm.query
().find() items.forEach { println(it.summary) } ``` --- # Consultar - Flow ``` realm.query
().find().asFlow().collect{ println(it.list.map{it.summary}) println() } launch { repeat(3){ realm.writeBlocking { val item = Item(summary = "Local Only Some summary ${Date()}") copyToRealm(item) } delay(5000) } realm.close() } ```