objectbox入门demo(Kotlin版本)

作者: 蓝不蓝编程 | 来源:发表于2019-01-25 09:45 被阅读2次

    objectbox介绍

    objectbox是一款嵌入式数据库,最大的特点是快,号称最快,特别是查询速度。完成优点罗列如下:

    1. 超快 objectbox优于测试过的所有嵌入式数据库
    2. 面向对象的api 没有ORM,没有SQLite,简洁的api
    3. 自动升级 可以直接在对象上进行删除修改等操作

    如何使用

    demo源代码:https://gitee.com/cxyzy1/objectboxDemo

    1. 项目根目录下build.gradle文件配置:
    buildscript {
        ext.kotlin_version = '1.3.20'
        ext.objectboxVersion = '2.3.1'
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.3.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
        }
    }
    
    1. app目录下build.gradle文件配置
      1). 顶部增加:
    apply plugin: 'io.objectbox'
    

    2). dependencies中增加:

        implementation "io.objectbox:objectbox-android:$objectboxVersion"
        implementation "io.objectbox:objectbox-kotlin:$objectboxVersion"
        kapt "io.objectbox:objectbox-processor:$objectboxVersion"
    
    1. 同步一下工程
    2. 创建数据类
    import io.objectbox.annotation.Entity
    import io.objectbox.annotation.Id
    @Entity
    data class Note(
        @Id var id: Long = 0,
        var text: String? = null,
        var comment: String? = null
    )
    
    1. 创建ObjectBox.kt
    import android.content.Context
    import android.os.Environment
    import io.objectbox.BoxStore
    import java.io.File
    object ObjectBox {
        lateinit var boxStore: BoxStore
            private set
    
        fun build(context: Context) {
            // This is the minimal setup required on Android
            boxStore = MyObjectBox.builder().androidContext(context.applicationContext).build()
    
            // Example how you could use a custom dir in "external storage"
            // (Android 6+ note: give the app storage permission in app info settings)
    //        val directory = File(Environment.getExternalStorageDirectory(), "objectbox-notes");
    //        boxStore = MyObjectBox.builder().androidContext(context.applicationContext)
    //                .directory(directory)
    //                .build()
        }
    }
    
    1. Application中增加初始化
    ObjectBox.build(this)
    
    1. activity中使用样例:
    package com.cxyzy.demo
    
    import android.os.Bundle
    import android.support.v7.app.AppCompatActivity
    import android.support.v7.widget.LinearLayoutManager
    import android.widget.Toast
    import io.objectbox.Box
    import io.objectbox.kotlin.boxFor
    import io.objectbox.kotlin.query
    import io.objectbox.query.Query
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
        private lateinit var notesBox: Box<Note>
        private lateinit var notesQuery: Query<Note>
        private lateinit var notesAdapter: NotesAdapter
        private val dataList: MutableList<Note> = mutableListOf()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            notesBox = ObjectBox.boxStore.boxFor()
            notesQuery = notesBox.query {
                order(Note_.text)
            }
            loadData()
            initViews()
            addIv.setOnClickListener { addNote() }
        }
    
        private fun initViews() {
            notesAdapter = NotesAdapter(this, dataList)
            with(recyclerView)
            {
                layoutManager = LinearLayoutManager(this@MainActivity)
                adapter = notesAdapter
            }
            notesAdapter.onItemClickListener = object : OnItemClickListener {
                override fun OnItemClick(position: Int) {
                    Toast.makeText(this@MainActivity, dataList[position].text, Toast.LENGTH_LONG).show()
                }
            }
    
        }
    
        private fun addNote() {
            val note = Note(text = "text", comment = "comment")
            notesBox.put(note)
            loadData()
            notesAdapter.notifyDataSetChanged()
        }
    
        private fun loadData() {
            val notes = notesQuery.find()
            dataList.clear()
            dataList.addAll(notes)
        }
    }
    
    

    参考资料

    官网地址 http://objectbox.io
    github地址 https://github.com/objectbox/objectbox-java
    https://www.jianshu.com/p/24b7ffbbe383

    安卓开发技术分享: https://www.jianshu.com/p/442339952f26

    相关文章

      网友评论

        本文标题:objectbox入门demo(Kotlin版本)

        本文链接:https://www.haomeiwen.com/subject/hxxmjqtx.html