I'm in situation where i have other model classes nested inside a parent class , and the thing is that i want to insert the model classes into room db but it is asking me to use type converter but i don't know to and from which type should i convert the data class , this is my code
data class FixturesMergedModels (
#Embedded var event: Event,
#Embedded var teamXX: TeamXX,
#Embedded var awaylogo: testAwayLogoModel
)
My Event Model
#Entity
data class Event(
#PrimaryKey(autoGenerate = true)
var evenid : Int?,
#SerializedName("dateEvent")
var dateEvent : String?,
#SerializedName("strTime")
val strTime : String?,
#SerializedName("strThumb")
val strThumb : String?,
#SerializedName("strHomeTeam") //
val strHomeTeam : String?,
#SerializedName("strAwayTeam") //
val strAwayTeam : String?,
#SerializedName("idAwayTeam")
val idAwayTeam: Int?,
#SerializedName("idEvent")
val idEvent: Int?,
#SerializedName("idHomeTeam")
val idHomeTeam : Int?,
#SerializedName("intHomeScore") //
val intHomeScore : String?,
#SerializedName("intAwayScore") //
val intAwayScore : String?
)```
* Home Team Model ( teamxx)
#Entity
data class TeamXX(
#PrimaryKey(autoGenerate = true)
var homeTeamID : Int?,
#SerializedName("strTeamBadge")
val HomestrTeamBadge: String?
)```
Away Team Model
data class testAwayLogoModel (
#PrimaryKey(autoGenerate = true)
var awayTeamID : Int?,
#SerializedName("strTeamBadge")
val AwaystrTeamBadge: String?
)
According to the google developer website, you should use #Embedded for your event and teamXX class. Here is a short example given at their website.
data class Address(
val street: String?,
val state: String?,
val city: String?,
#ColumnInfo(name = "post_code") val postCode: Int)
#Entity
data class User(
#PrimaryKey val id: Int,
val firstName: String?,
#Embedded val address: Address?
)
for more detail you can visit here: https://developer.android.com/training/data-storage/room/relationships
Related
in my project , I have two Data Model class
first :
#Entity(tableName = TABLE_TASK)
data class TaskEntity(
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "task_id") val taskId: Int,
#ColumnInfo(name = "task_title") val taskTitle: String?,
#ColumnInfo(name = "task_desc") val taskDesc: String?,
#ColumnInfo(name = "task_priority") val taskPriority: Int?,
#ColumnInfo(name = "task_status") val taskStatus: String?,
#ColumnInfo(name = "task_time") val taskTime: String?,
#ColumnInfo(name = "task_notify") val taskNotify: String?
)
and second :
data class SubtaskModel(
val subTasktitle : String?,
val subTaskStatus : Boolean?
)
and I wanted to add a filed in my first Data Model class as the code below (the last field ) :
#Entity(tableName = TABLE_TASK)
data class TaskEntity(
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "task_id") val taskId: Int,
#ColumnInfo(name = "task_title") val taskTitle: String?,
#ColumnInfo(name = "task_desc") val taskDesc: String?,
#ColumnInfo(name = "task_priority") val taskPriority: Int?,
#ColumnInfo(name = "task_status") val taskStatus: String?,
#ColumnInfo(name = "task_time") val taskTime: String?,
#ColumnInfo(name = "task_notify") val taskNotify: String?,
#ColumnInfo(name = "task_subtask") val taskSubTask: MutableList<SubtaskModel>?
)
the error that i have got :
Cannot figure out how to save this field into database. You can
consider adding a type converter for it.
so I read(developer.android.com) and I did search about this but I couldn't figure out how can write Type Converter.
thank you
Follow below steps :
Step1. : create a convertor class -
class MyConverter {
#TypeConverter
fun listToJson(value: List< SubtaskModel >?): String = Gson().toJson(value)
#TypeConverter
fun jsonToList(value: String) = Gson().fromJson(value, Array<SubtaskModel>::class.java).toList()
}
Step 2 -> Add converter to your dataBase class
#Database(entities = [TaskEntity::class], version = 1)
#TypeConverters(MyConverter::class)
abstract class myDataBase : RoomDatabase() {
abstract fun myDao(): MyDao
}
That's it.
I'm trying to save the data class i got from json to room database and suddenly get errors like this while building the app.
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).
NewsResponse model object
data class NewsResponse(
val copyright: String,
val response: Response,
val status: String
)
Response object
data class Response(
val docs: List<Docs>
)
Docs object(this is the data class i want to save)
#Entity(
tableName = "docs"
)
data class Docs(
#PrimaryKey(autoGenerate = true)
var id: Int? = null,
val _id: String,
val `abstract`: String,
#Embedded
val byline: Byline,
val document_type: String,
#Embedded
val multimedia: Multimedia,
#Embedded
val headline: Headline,
val lead_paragraph: String,
val news_desk: String,
val pub_date: String,
val section_name: String,
val snippet: String,
val source: String,
val subsection_name: String,
val type_of_material: String,
val uri: String,
val web_url: String,
val word_count: Int
)
Byline object
#Entity
data class Byline(
val original: String
)
Multimedia object
#Entity
data class Multimedia(
val url: String
)
Headline object
#Entity
data class Headline(
val main: String
)
Any ideas how to solve this?
I have entity
#Entity
data class SegmentEntity(
#PrimaryKey(autoGenerate = true)
val id: Long,
var rideRelatedId: Long?,
var name: String?,
var speed: String?
)
And I wish that PrimaryKey will be autogenerated, but when I build this entry I require to add it to constructor:
SegmentEntity(0,3,"Dan","90")
How can I autogenerate ids without adding it constructor
you can change your data class to bellow and put your id out of contractor:
#Entity
data class SegmentEntity(
var rideRelatedId: Long?,
var name: String?,
var speed: String?
) {
#PrimaryKey(autoGenerate = true)
val id: Long? = null;
}
or can set default value null to your id look like this:
#Entity
data class SegmentEntity(
#PrimaryKey(autoGenerate = true)
val id: Long? = null,
var rideRelatedId: Long?,
var name: String?,
var speed: String?
)
in this case you must call your class :
SegmentEntity(name = "nameValue",rideRelatedId = 1 ,speed ="speedValue")
Your model should look like this :
#Entity
data class SegmentEntity(
#PrimaryKey(autoGenerate = true)
val id: Long = 0L,
var rideRelatedId: Long?,
var name: String?,
var speed: String?
And then you can do SegmentEntity(3,"Dan","90") with incremented id after insertion in your database
I think that this is currently best practice to do this:
#Entity
data class SegmentEntity(
var rideRelatedId: Long?,
var name: String?,
var speed: String?
) {
#PrimaryKey(autoGenerate = true)
var id: Int? = null
}
Issue Basics
ObjectBox version 2.5.1
Reproducibility: [always]
Hi, I am getting this error everytime I want to load my Object even though on saving I checked in the debugger and see that the relation target is not null and everything is saved correctly.
I am having those trhee entity classes
#Entity
data class NetflixCache (
val results: List<NetflixSearchResult>,
#Id var objectBoxId: Long?
) {
var searchParams: ToOne<SearchParams> = ToOne<SearchParams>(this, NetflixCache_.searchParams)
}
#Entity
data class SearchParams(val genrelist: String,
val type: String,
val imdbRatingMin: String,
val countryId: String,
val startYear: Int,
val endYear: Int,
val offset: Int? = 0,
val timeStamp: Long,
#Id var id: Long? = null)
#Entity
data class NetflixSearchResult (
val vtype: String?,
val img: String?,
val nfid: Long?,
val imdbid: String?,
val title: String?,
val clist: String?,
val poster: String?,
val imdbrating: Double?,
val synopsis: String?,
val titledate: Date?,
val year: Int?,
var id: Long,
#Id var objectBoxId: Long?
)
Using this code to save:
val cacheBox = LetsFlixApplication.boxStore.boxFor(NetflixCache::class.java)
val netflixCache = NetflixCache(searchResults, null)
netflixCache.searchParams.target = searchParams
cacheBox.put(netflixCache)
And this code to load:
val cachedResult = cacheBox.all //<-Exception is thrown here
As you can see in debugger when saving, ToOne Object is NOT null:
https://ibb.co/s3tdhqP
This is the exact exception message I am getting:
ToOne object is null inside com.haenles.letsflix.domain.NetflixCache
Issue for me was that data class was missing default values as recommended in the doc:
#Entity
data class User(
#Id var id: Long = 0,
var name: String? = null
)
I'm downloading few lists (RawAlbum, RawPhoto) from the web and I want to use properties from them to create another list. Then I want to use it to make a recyclerview.
So that's how it looks. It is the ListItem:
#Parcelize
data class ListItem(
val id: Int,
val title: String,
val albumTitle: String,
val thumbnailUrl: String
) : Parcelable
So I want to use the id and title from the RawAlbum. While the albumTitle and thumbnailUrl from the RawPhoto.
RawAlbum:
#Parcelize
#Entity(tableName = "albums")
data class RawAlbum(
#PrimaryKey
val id: Int,
val userId: Int,
val title: String
) : Parcelable
RawPhoto:
#Parcelize
#Entity(tableName = "photos")
data class RawPhoto(
#PrimaryKey
val id: Int,
val albumId: Int,
val title: String,
val url: String,
val thumbnailUrl: String
) : Parcelable