There are multiple good constructors and Room will pick the no-arg constructor. How to solve this warning

萝らか妹 提交于 2020-01-23 09:31:54

问题


I try to implement Room Persistent Database library in android kotlin project, but catch this warning at compile time. I don't know how to solve this warring.

warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.

Auto Generated Class

public final class Gender {
             ^

Kotlin Data Class

import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey

@Entity
data class Gender(@PrimaryKey(autoGenerate = true)
             var genderId: Int = 0,
             var type: String = "")

回答1:


Try this:

import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey

@Entity
class Gender @Ignore constructor(@PrimaryKey(autoGenerate = true)
             var genderId: Int = 0,
             var type: String = "") {

    constructor() : this(0, "")
}

Like the warning says

(...) Room will pick the no-arg constructor. (...)

Your constructor does have two arguments. You needed to add an empty one and ignore the other




回答2:


The "problem" here is that kotlin is generating multiple constructors for your class, given that you have default parameters for some of the properties.

In your case you have:

// this is the synthetic one, don't worry to much about it
public Gender(int var1, String var2, int var3, DefaultConstructorMarker var4) { /* some implementation */ }

// the "default" one, that can be called when you are delegating to the default params
public Gender() { /* some implementation */ }

// the one that gets all the params
public Gender(int genderId, @NotNull String type) { /* some implementation */ }

Room could be using either the one without parameters, or the one with two, and it's choosing one of them (and letting you know via that warning)

You can remove the default parameter for type, and there will be only one (non-synthetic) constructor:

// still synthetic
public Gender(int var1, String var2, int var3, DefaultConstructorMarker var4) { /* some implementation */ }

// this is the only usable constructor now
public Gender(int genderId, @NotNull String type) { /* some implementation */}

Now Room has only one constructor to use, so it will use it happily.

If your use case allows it, you can just remove the default values. Note that you could only do it for the non-primitive types, which make make your API nicer.

I don't know about your particular case, but note that you could also use val instead of var

@Entity
data class Gender(
    @PrimaryKey(autoGenerate = true)
    val genderId: Int = 0,  // so callers don't need to specify an id. Room will generate one if it gets a 0 here
    var type: String
)


来源:https://stackoverflow.com/questions/52983393/there-are-multiple-good-constructors-and-room-will-pick-the-no-arg-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!