ObjextBox usage in kotlin

Kanan Abilzada
2 min readNov 25, 2022

--

ObjectBox is a fast NoSQL database solution for storing your data and getting it fastly. I’ll add it kotlin project. For adding it follow the instructions below (you also find them on your own site).

  1. Open the root build.gradle
buildscript {
ext.objectboxVersion = "3.4.0"
repositories {
mavenCentral()
// Note: 2.9.0 and older are available on jcenter()
}
dependencies {
// Android Gradle Plugin 3.3.0 or later supported.
classpath("com.android.tools.build:gradle:7.2.2")
classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion")
}
}

2. Open the build.gradle file for your app or module

// Using plugins syntax:
plugins {
id("com.android.application")
id("kotlin-android") // Only for Kotlin projects.
id("kotlin-kapt") // Only for Kotlin projects.
id("io.objectbox") // Apply last.
}

// Or using the old apply syntax:
apply plugin: "com.android.application"
apply plugin: "kotlin-android" // Only for Kotlin projects.
apply plugin: "kotlin-kapt" // Only for Kotlin projects.
apply plugin: "io.objectbox" // Apply last.

Let’s create an Entity class:

@Entity
data class MyEntity(
@Id
var id: Long = 0,
var name: String? = null
)

ObjextBox Docs recommends adding default values for parameters. We can use another types for id.

Now I am going to create an ObjectBox object that will reference our database and manage our boxes:

object ObjectBox {
lateinit var store: BoxStore
private set

fun init(context: Context) {
store = MyObjectBox.builder()
.androidContext(context.applicationContext)
.build()
}
}

Now you’ll get an error like Unresolves reference: MyObjectBox , to fix that we must build a project. Go to Build > Make Project
If you open => app/objectbox-models/default.json you’ll see that the JSON file keeps your entity information. Keep it with your project.

Docs says:

This file keeps track of unique IDs assigned to your entities and properties. This ensures that an older version of your database can be smoothly upgraded if your entities or properties change.

Okay, ObjextBox does not return an error now. We must start it when the application starts. Open oncreate method of the Application Class

class ExampleApp : Application() {
override fun onCreate() {
super.onCreate()
ObjectBox.init(this)
}
}

Done, we can use it now.

  1. Add New Data to the entity:
val myEntityBox = ObjectBox.store.boxFor(MyEntity::class.java)

val newData = MyEntity("New Data")

// put inserts a new object or updates an existing one (with the same ID)
myEntityBox.put(newData)

2. Removing data:

val item = MyEntity("New Data")

myEntityBox.remove(item)

3. Set a query to return objects from the box that match certain conditions:

val query = myEntityBox
.query(MyEntity_.name.equal("Something"))
.order(MyEntity_.name)
.build()
val results = query.find()
query.close()

And more you find on ObjextBox's own site.

Conclusion

ObjectBox is a fast and easy to use database for small devices compared to other databases. You can use it in many languages like Swift, Java, Dart/Flutter, and more, Add relationships between entities, observe entities, migrations, etc. For more visit https://docs.objectbox.io/

Thanks For Reading :)

--

--