ObjectBox 的基础使用(Kotlin 版本)

project 的 gradle:

buildscript {
    ext.kotlin_version = '1.1.3-2'
    ext.objectboxVersion = '1.1.0'
    repositories {
        google()
        jcenter()
        maven { url "http://objectbox.net/beta-repo/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta7'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "http://objectbox.net/beta-repo/" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app 的 gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'io.objectbox'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "me.palorotolo.objectboxdemo"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'build/generated/source/objectbox'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    // ObjectBox
    compile "io.objectbox:objectbox-android:$objectboxVersion"
    annotationProcessor "io.objectbox:objectbox-processor:$objectboxVersion"
    compile "io.objectbox:objectbox-kotlin:$objectboxVersion"
    kapt "io.objectbox:objectbox-processor:$objectboxVersion"

}

Bean:

@Entity
data class User(
        // 如果指定 id 默认为 0,则 id 会从 1 开始自增
        @Id var id: Long = 0,
        var name: String
)

Application 中初始化 BoxStore 对象:

class App: Application() {
    
    override fun onCreate() {
        super.onCreate()

        initObjectBox()
    }

    lateinit var boxStore: BoxStore
    private fun initObjectBox() {
        boxStore = MyObjectBox.builder().androidContext(this).build()
    }

}

对象的初始化:

lateinit var userBox: Box<User>
private fun init() {
    // 操作数据的对象
    userBox = (application as App).boxStore.boxFor(User::class.java)

    // 清除所有数据
    userBox.removeAll()
}

查询和添加:

fun add(view: View) {
    var user = User(name = "some name")
    // 添加数据
    userBox.put(user)
}

fun query(view: View) {
    // 查询所有数据
    var users = userBox.query().build().find()
    for (user in users) {
        Log.d("MainActivity", user.toString())
    }
}

默认是主线程添加的,没有异步线程的添加方法?

Unresolved reference: MyObjectBox
因为没有在 app 的 gradle 加上:

apply plugin: 'kotlin-kapt'
    原文作者:Obadiah
    原文地址: https://www.jianshu.com/p/2f578a9749e1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞