Kotlin Realim 사용하는 방법
11 Apr 2019 | Android Kotlinbuild.gradle(프로젝트)
buildscript {
ext.kotlin_version = '1.3.21'
ext.anko_version = '0.10.8'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:5.10.0"
}
}
build.gradle(모듈)
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' apply plugin: 'realm-android'
Realm 객체 생성하기
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Todo(@PrimaryKey var id: Long = 0,
var title: String = "",
var date: Long = 0) : RealmObject() {
}
SnowApplication.kt
import android.app.Application
import io.realm.Realm
class SnowApplication : Application() {
override fun onCreate() {
super.onCreate()
Realm.init(this)
}
}
그리고 AndroidManifest.xml에 위 Application을 등록합니다.
MainActivity.kt
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import io.realm.Realm
import io.realm.Sort
import io.realm.kotlin.createObject
import io.realm.kotlin.where
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.yesButton
import java.util.*
class MainActivity : AppCompatActivity() {
private val realm = Realm.getDefaultInstance()
private val calendar = Calendar.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
insert_button.setOnClickListener { insertItem() }
select_button.setOnClickListener { selectItemList() }
update_button.setOnClickListener { updateItem(1) }
delete_button.setOnClickListener { deleteItem(1) }
}
override fun onDestroy() {
super.onDestroy()
realm.close()
}
private fun nextId(): Long {
val maxId = realm.where<Todo>().max("id")
if (maxId != null) {
return maxId.toLong() + 1
}
return 0
}
private fun insertItem() {
realm.beginTransaction()
val newItem = realm.createObject<Todo>(nextId())
newItem.title = "Title"
newItem.date = calendar.timeInMillis
realm.commitTransaction()
alert("An item is added.") {
yesButton { }
}.show()
}
private fun selectItemList() {
val realmResult = realm.where<Todo>().findAll().sort("date", Sort.DESCENDING)
for (Todo in realmResult) {
Log.i("snowdeer", "[snowdeer] todo: $Todo")
}
}
private fun updateItem(id: Long) {
realm.beginTransaction()
val newItem = realm.where<Todo>().equalTo("id", id).findFirst()!!
newItem.title = "Title is changed !!!"
newItem.date = calendar.timeInMillis
realm.commitTransaction()
alert("An item($id) is changed.") {
yesButton { }
}.show()
}
private fun deleteItem(id: Long) {
realm.beginTransaction()
val item = realm.where<Todo>().equalTo("id", id).findFirst()!!
item.deleteFromRealm()
realm.commitTransaction()
alert("An item($id) is deleted.") {
yesButton { }
}.show()
}
}