objectbox入门demo(Kotlin版本)
2019-01-25 本文已影响2人
蓝不蓝编程
objectbox介绍
objectbox是一款嵌入式数据库,最大的特点是快,号称最快,特别是查询速度。完成优点罗列如下:
- 超快 objectbox优于测试过的所有嵌入式数据库
- 面向对象的api 没有ORM,没有SQLite,简洁的api
- 自动升级 可以直接在对象上进行删除修改等操作
如何使用
demo源代码:https://gitee.com/cxyzy1/objectboxDemo
- 项目根目录下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"
}
}
- 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"
- 同步一下工程
- 创建数据类
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
)
- 创建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()
}
}
- Application中增加初始化
ObjectBox.build(this)
- 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