0

我有一个简单的用户数据类

data class User(@PrimaryKey(autoGenerate = true) val id: Long, val _id: String, val name: String, val about: String,
                    val phoneNumber: String, val token: String,
                    val lastLogin: String, val avatarUrl: String, @Embedded val location: Location,
                    val emailId: String, val gender: Boolean, val timestamp: Long = System.currentTimeMillis())

如您所见,最后一个参数是 val timestamp: Long = System.currentTimeMillis()

响应来自使用改造的网络并使用解析GSON

timestamp没有响应 json 它只是我需要做一些逻辑的额外字段。问题是值总是0. 它应该是当前时间戳

4

1 回答 1

1

这会成功的,

data class User(@PrimaryKey(autoGenerate = true) val id: Long, val _id: String, val name: String, val about: String,
                val phoneNumber: String, val token: String,
                val lastLogin: String, val avatarUrl: String, @Embedded val location: Location,
                val emailId: String, val gender: Boolean) {
    var timestamp: Long = System.currentTimeMillis()
    get() = if(field > 0) field else {
        field = System.currentTimeMillis()
        field
    }
}
于 2018-08-17T12:13:44.567 回答