Basic Realm + Kotlin:RealmObject没有设置传递的值

时间:2017-02-03 20:22:38

标签: android realm kotlin

我最近开始使用Realm和Kotlin,但我无法在DB上进行简单的插入。以下是我使用的代码:

MODEL

open class User : Serializable, RealmObject() {
    open var user_id: Long = 0
    open var name: String = ""
    open var nickname: String = ""
    open var thumbnail: String = ""
    open var password: String = ""
    open var email: String = ""
    open var token: String = ""
    open var country_code: String = ""
    open var image_profile_original: String = ""
    open var distance: Double = 0.toDouble()
    open var birth_date: Date = Date()

    @Ignore
    open var status: UserStatus = UserStatus.OFFLINE
    @Ignore
    open var type: UserType = UserType.OTHER
    @Ignore
    open var gender: Gender = Gender.male
    @Ignore
    open var game_objects = mutableListOf<Game>()
    @Ignore
    open var games = mutableListOf<Long>()
    @Ignore
    open var friends: RealmList<User>? = null
    @Ignore
    open var friends_request: RealmList<User>? = null

    override fun toString(): String {
        return Gson().toString()
    }

    fun toCanvasUser(): br.com.edsilfer.android.messenger.model.User {
        val canvasUser: br.com.edsilfer.android.messenger.model.User = br.com.edsilfer.android.messenger.model.User()
        canvasUser.id = user_id.toDouble()
        canvasUser.name = name!!
        canvasUser.nickname = nickname!!
        canvasUser.thumbnail = image_profile_original!!
        return canvasUser
    }

    companion object {
        fun fromCanvasUser(canvasUser: br.com.edsilfer.android.messenger.model.User): User {
            val user = User()
            return user
        }
    }
}

在Applicaion类

private fun initRealm() {
    Realm.init(this)
    val config = RealmConfiguration.Builder()
            .deleteRealmIfMigrationNeeded()
            .build()
}

DAO模式

object UserDAO {

    val TAG = UserDAO::class.simpleName
    val mRealm = Realm.getDefaultInstance()

    fun create(user: User) {
        mRealm.beginTransaction()
        mapObject(user)
        mRealm.commitTransaction()
        Log.i(TAG, "Realm persisted object: $user successfully}")
    }

    fun read(id: Long): User {
        val user = mRealm.where(User::class.java).equalTo("user_id", id).findFirst()
        Log.i(TAG, "Realm read object: $user")
        return user
    }

    fun list(): RealmResults<User> {
        val realm = Realm.getDefaultInstance()
        return realm.where(User::class.java).findAll()
    }

    fun update(user: User) {
        mRealm.beginTransaction()
        mRealm.copyToRealmOrUpdate(user)
        mRealm.commitTransaction()
        Log.i(TAG, "Realm updated object: $user")
    }

    fun delete(id: Long) {
        mRealm.beginTransaction()
        val user = mRealm.where(User::class.java).equalTo("user_id", id).findAll()
        user.clear()
        Log.i(TAG, "Realm removed object: $user")
        mRealm.commitTransaction()
    }

    fun deleteAll() {
        mRealm.beginTransaction()
        val results = mRealm.where(User::class.java).findAll()
        results.clear()
        mRealm.commitTransaction()
    }

    private fun mapObject(user: User) {
        val realmUser = mRealm.createObject(User::class.java)
        realmUser.name = user.name
        realmUser.user_id = user.user_id
        realmUser.nickname = user.nickname
        realmUser.thumbnail = user.thumbnail
        realmUser.password = "sample password"
        realmUser.email = user.email
        realmUser.token = user.token
        realmUser.country_code = user.country_code
        realmUser.image_profile_original = user.image_profile_original
        realmUser.status = user.status
        realmUser.type = user.type
        realmUser.birth_date = user.birth_date
        realmUser.gender = user.gender
        realmUser.distance = user.distance
        realmUser.game_objects = user.game_objects
        realmUser.games = user.games
        realmUser.friends = user.friends
        realmUser.friends_request = user.friends_request
    }
}

问题出在mapObject函数中。即使填充了参数user,对象realmUser也不会将传递的值赋给它。有什么想法吗?

0 个答案:

没有答案