Newer
Older
DevRealm / app / src / main / java / jackall / devrealm / db / PersonRealmHelper.kt
MatsumotoKeiju on 8 Nov 2017 786 bytes [fix] Return null when db is empty
package jackall.devrealm.db

import io.realm.RealmResults
import jackall.devrealm.vo.Person

/**
 * Created by matsumoto_k on 2017/11/08.
 */
class PersonRealmHelper : BaseRealmHelper<Person>() {
    val query by lazy { realm.where(Person::class.java) }
    override fun insert(realmObject: Person) {
        realm.executeTransaction {
            realm.copyToRealm(realmObject)
        }
    }

    override fun delete(realmObject: Person) {
        realm.executeTransaction {
            realmObject.deleteFromRealm()
        }
    }

    override fun findAll(): RealmResults<Person> {
        return query.findAll()
    }

    fun findLast(): Person? {
        val results = query.findAll()
        if (results.isEmpty())
            return null
        return results.last()
    }
}