막무가내 삽질 블로그

Tried the following constructors but they failed to match 본문

Android

Tried the following constructors but they failed to match

joong~ 2020. 9. 27. 23:56
728x90

룸에서 필드를 만들고 싶지 않은 경우 @Ignore를 붙여서 필드를 만들지 않는다.

 

하지만 생성자 안에서 @Ignore를 붙힐 경우 생성자 매치 오류를 만나게 된다.

 

코틀린에서는 생성자 기본값이라는 게 있고 자바는 없는 개념이다.

 

그러한 이유로 값을 넣을 때 순서관련 이슈로 보인다.

 

@Entity(tableName = "movie")
data class Movie(
    @PrimaryKey(autoGenerate = false)
    @SerializedName("id")
    @Expose
    val id: Int,
    
    @SerializedName("url")
    @Expose
    val url: String,
    
    @SerializedName("title")
    @Expose
    val title: String,
    
    @SerializedName("year")
    @Expose
    val year: Int,
    
    @SerializedName("rating")
    @Expose
    val rating: Float?,
    
    @SerializedName("genres")
    @Expose
    @Ignore
    val genres: List<String>,
    
    @SerializedName("summary")
    @Expose
    val summary: String,
    
    @SerializedName("medium_cover_image")
    @Expose
    val poster: String,
    
    var hasLiked: Boolean = false
)

-> Tried the following constructors but they failed to match 에러 발생

 

 

 

해결방법 1   -> outside로 해결

@Entity(tableName = "movie")
data class Movie(
    @PrimaryKey(autoGenerate = false)
    @SerializedName("id")
    @Expose
    val id: Int,

    @SerializedName("url")
    @Expose
    val url: String,

    @SerializedName("title")
    @Expose
    val title: String,

    @SerializedName("year")
    @Expose
    val year: Int,

    @SerializedName("rating")
    @Expose
    val rating: Float?,

    @SerializedName("summary")
    @Expose
    val summary: String,

    @SerializedName("medium_cover_image")
    @Expose
    val poster: String,

    var hasLiked: Boolean = false
) {
    @SerializedName("genres")
    @Expose
    @Ignore
    val genres: List<String>? = null
}

 

해결방법 2 -> ignore된 필드가 없는 생성자가 필요로 함(디테일한건 바이트코드로 바꿔보면 알 수 있다, @JvmOverloads 있고 없고)

@Entity(tableName = "movie")
data class Movie @JvmOverloads constructor(
    @PrimaryKey(autoGenerate = false)
    @SerializedName("id")
    @Expose
    val id: Int,
    
    @SerializedName("url")
    @Expose
    val url: String,
    
    @SerializedName("title")
    @Expose
    val title: String,
    
    @SerializedName("year")
    @Expose
    val year: Int,
    
    @SerializedName("rating")
    @Expose
    val rating: Float?,
    
    @SerializedName("genres")
    @Expose
    @Ignore
    val genres: List<String>? = null,
    
    @SerializedName("summary")
    @Expose
    val summary: String,

    @SerializedName("medium_cover_image")
    @Expose
    val poster: String,

    var hasLiked: Boolean = false
)

 

 

'Android' 카테고리의 다른 글

Migrating from Fabric to Firebase Crashlytics  (0) 2020.10.04
RxJava 4주차  (0) 2020.09.28
RxJava 3주차  (0) 2020.09.20
RxJava 2주차  (0) 2020.09.14
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution  (1) 2020.09.07
Comments