Day98 of #100DaysOfCode

Kushagra Kesav
2 min readMay 15, 2022

--

Day98 of #100DaysOfCode

Hii folks 🙌

Today I will be starting a new pathway in which we will learn about databases.

Unit 5: Introduction to Databases

Realm Basic

Let’s talk about databases in Android. Realm is an object database that is simple to embed in your mobile app. Realm is a developer-friendly alternative to mobile databases such as SQLite and CoreData.

What Problem Does Realm Database Solve?

Most applications of these times will need some persistent data. Building such applications involves making the application more complex and adding more lines of code. Now, we all know the increased pain and work that goes behind developing such complex apps which results in longer development cycles, overburdened resources, and an unwieldy code base.

As a developer, we face a number of similar challenges during the development of any mobile application. The Realm database is the object-based database that will make it easier for us, especially in such complex development projects.

Realm helps developers in gracefully handling the unpredictable environment of mobile apps in which devices can shut down at any time; connections can be lost, etc. It provides an opportunity to coordinate between mobile clients, backend APIs, and databases.

Benefits of using Realm:

  • Build apps faster
  • Build bigger & better apps
  • Reduce experience latency & server bills
  • Get “offline mode” for free
  • Craft experiences to a higher level
  • Program for more than one app platform

Local Realm Database

With the Realm Kotlin SDK, we can access objects stored in a local instance of the Realm Database. With Realm Database, we can

  • Define an Object Schema

Define the object schema with marked Kotlin classes:

class Frog : RealmObject {    
var name: String = ""
var age: Int = 0
var species: String? = null
var owner: String? = null
}
  • Query Realm Database

The query for stored objects:

val config = RealmConfiguration.Builder(schema = setOf(Frog::class))
.build()
val realm = Realm.open(config)
val tadpoles: RealmQuery<Frog> = realm.query<Frog>("age > $0", 2)
Log.v("Tadpoles: ${tadpoles.count()}")
val numTadpolesNamedJasonFunderburker = tadpoles.query("name == $0", "Jason Funderburker").count()
Log.v("Tadpoles named Jason Funderburker: $numTadpolesNamedJasonFunderburker")
  • Update Objects

Update objects in the Realm Database by updating field values on an instance of the object within a transaction:

val config = RealmConfiguration.Builder(schema = setOf(Frog::class))
.build()
val realm = Realm.open(config)
// start a write transaction
realm.writeBlocking {
// get a frog from the database to update
val frog: Frog? = query<Frog>()
.query("name == $0 LIMIT(1)",
"Benjamin Franklin")
.first()
.find()
// update the frog's properties
frog?.apply {
name = "George Washington"
species = "American bullfrog"
}
} // when the transaction completes, the frog's name and species
// are updated in the database

That is all for Day98 ✅

Thanks for reading, See you tomorrow!

--

--

Kushagra Kesav
Kushagra Kesav

Written by Kushagra Kesav

Developer Relations | Communities | Software Engineering | https://www.linkedin.com/in/kushagrakesav

No responses yet