Android Kotlin:实现多对一关系

时间:2018-11-03 18:00:25

标签: android kotlin realm

在使用Realm进行Kotlin对象之间的一对多关系管理时,我遇到了麻烦。我不知道如何进行一些插入或更新。

这是我的两个对象,它们与考试和职业之间存在一对多的关系。

open class Career : RealmObject() {

    @PrimaryKey
    var id: Long = 1
    var status: String = CareerStatus.ACTIVE.value
    var careerName: String? = null
    var exams: RealmList<Exam>? = null
}

open class Exam : RealmObject() {

    @PrimaryKey
    var id: Long = 1
    var teachers: String? = null
    var lode: Boolean? = null
    var score: String = ""
    var exam: String = ""
    var examDescription: String? = null
    var date: Date = Date()
    var cfu: Long = Long.MIN_VALUE
    @LinkingObjects("exams")
    val career: RealmResults<Career>? = null
}

在这里,由两个班级管理者来管理对Realm的读/写

class CareerManager : DatabaseManager() {
    fun findActiveCareer(): Career? {
        return realm.where(Career::class.java).equalTo("status", CareerStatus.ACTIVE.value).findFirst()
    }

    fun insertExamInCareer(exam: Exam) {
        realm.executeTransaction {
            var activeCareer: Career = findActiveCareer()
                    ?: throw Resources.NotFoundException("No active career found")

            activeCareer.exams?.add(exam)
            realm.copyToRealmOrUpdate(exam)
        }
    }

    fun closeActiveCareer() {
        realm.executeTransaction {
            val activeCareer: Career = findActiveCareer()
                    ?: throw Resources.NotFoundException("No active career found")

            activeCareer.status = CareerStatus.TERMINATED.value
        }
    }
}

class ExamManager : DatabaseManager() {

    fun findAll(): List<Exam> {
        val findAll = realm.where(Exam::class.java).findAll()

        return realm.copyFromRealm(findAll).toList()
    }

    fun findAllActive(): List<Exam> {
        val findAll = realm.where(Exam::class.java).equalTo("career.status", CareerStatus.ACTIVE.value).findAll()

        return realm.copyFromRealm(findAll).toList()
    }

    fun insert(exam: Exam): Long {
        realm.beginTransaction()
        var newId: Long = 1

        if (realm.where(Exam::class.java).max("id") != null) {
            newId = realm.where(Exam::class.java).max("id") as Long + 2
        }

        val examToSave = realm.createObject(Exam::class.java, newId)
        examToSave.exam = exam.exam
        examToSave.examDescription = exam.examDescription
        examToSave.date = exam.date
        examToSave.teachers = exam.teachers
        examToSave.score = exam.score
        examToSave.lode = exam.lode
        examToSave.cfu = exam.cfu

        realm.commitTransaction()

        CareerManager().updateCareerExam(findAll())

        return newId

    }
}

在我的应用程序中,我需要在职业中插入一些实际上处于“活动”状态的考试。对于插入,我使用 CareerManager()。insertExamInCareer(exam)这个调用(在尝试使用 ExamManager()。insert(exam)但不起作用之前)。

另一个用例是当我需要结束一个职业并将考试添加到另一个职业中时。我以这种方式完成

CareerManager()。closeActiveCareer()

CareerManager()。createCareer()

CareerManager()。insertExamInCareer(考试) Here the result

两个职业的考试是相同的,但是必须不同,并且在考试中必须有两个记录,而不是一个。

0 个答案:

没有答案