0

I try to ignore a field when using the @Parcelize annotation in Kotlin so I am using @IgnoredOnParcel,

Plz guide me, on how to solve this error

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
    private final com.boltuix.mvvm.model.Source source = null;
                 ^

Here is my full source code: https://github.com/BoltUIX/REST-APIs-with-Retrofit-and-MVVM-architecture.

How to ignore fields when using @Parcelize annotation in Kotlin

Now i have a plan to recreate the data class, is there any better solution to ignore fields

@Entity(tableName = "favorite_movie")
@Parcelize
data class Article(
    val author: String?,
    val content: String?,
    val description: String?,
    val publishedAt: String?,
    val pagedLists: Source,
    val title: String?,
    val url: String?,
    val urlToImage: String?
): Parcelable {

    @PrimaryKey(autoGenerate = true)
    var id : Int = 0

    @IgnoredOnParcel
    var pagedList: Source = pagedLists

}

@IgnoredOnParcel annotation doesn't work for @Parcelize in Kotlin

............................. To ignore for Room, then you need to use Room's @Ignore annotation is not working for me

error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class Article implements android.os.Parcelable {..



@Entity(tableName = "favorite_movie")
@Parcelize
data class Article(
    val author: String?,
    val content: String?,
    val description: String?,
    val publishedAt: String?,
    @Ignore val pagedLists: Source,
    val title: String?,
    val url: String?,
    val urlToImage: String?
): Parcelable {

    @PrimaryKey(autoGenerate = true)
    var id : Int = 0

}
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58

1 Answers1

1

To ignore for Room, then you need to use Room's @Ignore annotation. It will then ignore the field as being a column and thus does not need to know how to save the field.

re

error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). public final class Article implements android.os.Parcelable {..

Room, if using @Ignore, does not know about the pagedLists and therefore MUST have a way to construct an Article without supplying the pagedLists value (as it doesn't have a value or the pagedLists field).

e.g. you could have something like :-

constructor(author: String?, content: String?, description: String?, publishedAt: String?, title: String?, url: String?, urlToImage: String?)
            : this(author = author, content = content,description =description, publishedAt = publishedAt, title = title, url = url, urlToImage = urlToImage, pagedLists = Source())
  • This assumes that you can construct the Source with no parameters (either ?'s or default values)

The alternative would be to have two classes on according to Room's columns, the other extending/embedding the first (without the pagedLists field) class.

MikeT
  • 51,415
  • 16
  • 49
  • 68