The use and detailed explanation of Android ObjectBox database

1. Introduction

Room database

        I have introduced the database of the jetpack component before: Room, and friends who need to know the Room database can check this address: Integration and detailed explanation of the Room database of the Android JetPack component_android room database_Snail, Z's Blog-CSDN Blog

        The performance of the database is very important to the device. We all know that the database is actually saved in a file format, and the content is searched through certain rules. But the equipment is different, and the purpose of selecting the database is naturally different.

Today I will introduce the recently popular database ObjectBox in the market, from access to use and a summary of problems

What is a mobile device database?

        The mobile database is an edge database that runs on mobile devices, that is, supports Android and iOS. It is an embedded database (a database embedded in an application) that is also optimized for high performance on constrained decentralized devices such as mobile devices.

ObjectBox database

        The objectBox database is a new product, and the bottom layer is a database engine completed in C and C++. It is a plug-in thing, and it is more complicated than Room access. This is a Nosql database, that is to say, this database can be used without SQL statements, and it is an operational database with objects as entities.

1. Official

Mobile Database | Android Database | Swift Database | Flutter Database

Access process

1. Introduce the compilation plug-in into the project

buildscript {
    ext.objectboxVersion = "3.6.0"

    dependencies {

        classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion")
    }
}

Description: The latest official version is 3.6.0

2. Module import plug-in

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

//或者 根据gradle高低版本选择构建方法
apply plugin: "kotlin-android" // Only for Kotlin projects.
apply plugin: "kotlin-kapt" // Only for Kotlin projects.
apply plugin: "io.objectbox" // Apply last.

Note: Because objectBox is written in kotlin language, the version is 1.7.20, so if your kotlin version is too low, you need to upgrade or, androidsudio between 22.0 needs to be upgraded to the latest version.

3. Code initialization, execute make project

object ObjectBox {
    lateinit var store: BoxStore
        private set

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

Notice:

1. Some people cannot find the MyObjectBox class in the code, because you need to build the project for the first time to access, and the plug-in can be automatically generated.

2. The initialization entry is in Application

table management

In objectBox, the table is maintained by the object, which needs to be obtained through the store.boxFor(cls) of BoxStore. The parameter class is the entity of the class of the current table.

1. Table creation


@Entity
class MyInfo {

    @Id
    var id: Long = 0
    var name: String? = ""
}

Description: The object modified by @Entity is an entity of a table, the field points to an id, and other modifications can be viewed by yourself

2. Acquisition of tables

   public fun <T> getDataBaseBox(cls: Class<T>):Box<T> {
        return store.boxFor(cls)
    }

Description: Get the table through generics, if it does not exist, it will be created

table operations

The operation of the table is actually very simple, and it is all done through objects.

//插入一条数据
    public fun <T> insertItem(t: T, cls: Class<T>): Long {

        return store.boxFor(cls).put(t)
    }
//批量插入数据
    public fun <T> insertList(t: MutableList<T>, cls: Class<T>) {

        store.boxFor(cls).put(t)
    }
//获取表中第一条数据

    public fun <T> getFirst(cls: Class<T>): T {

        return store.boxFor(cls).all.first()
    }
//获取表中的所有数据
    public fun <T> getAll(t: T, cls: Class<T>): MutableList<T> {

        return store.boxFor(cls).all
    }
//清空表

    public fun <T> getClean(cls: Class<T>) {
        store.boxFor(cls).removeAll()

    }
//删除单个数据
    public fun <T> getDeleteItem(t: T, cls: Class<T>) {
        store.boxFor(cls).remove(t)

    }

        These are all done based on objects, and some operations are done based on Id, keeping some queries is also based on query conditions, getting table objects, etc...

This table is automatically closed and resources are released, you only need to use it, other operations do not need you

3. Summary

If you are a novice, or Xiaobai. You can't maintain complex logic by yourself, and you don't need to take care of the closing of tables, etc.

Solutions to common problems

1. Error reporting

is not a known entity. Please add it and trigger generation again

Answer: It is because your model does not introduce plug-ins. Although your project integrates plug-ins, each module still needs to be imported separately. Like kotlin, the modules are independent. Otherwise, you create a table and report an error.

As long as this module needs to create a table, you need to import it into the module: id("io.objectbox"). After compilation, an objectbox-model folder will be generated, which contains your table information

2. Cannot find MyobjectBox

You must build the project or make if you have just joined the project. Otherwise, it will not be automatically generated and can only be used after generation

3. Prompt that the kotlin version is inconsistent

Because the kotlin version of objectBox is 1.7.20, and kotlin has a major revision after 1.7, your kotlin must be upgraded. Upgrading kotin may require an upgraded androidstudio file, so you must keep it up to date. Note that the kotlin version is: 1.7.20 , not 1.7.2, if you write 1.7.2 and cannot be updated, it will report an upgrade failure

Guess you like

Origin blog.csdn.net/qq36246172/article/details/131864280