How to represent nested #Relation with Android Room? - android

Here is my POJO in kotlin:
data class Pass(var uuid: String,
var activationRestrictions:List<ActivationRestriction>)
data class ActivationRestriction(var uuid: String,
var activationRestrictionExceptions:List<ActivationRestrictionException>)
data class ActivationRestrictionException(var uuid: String)
How can I represent this nested relation with Room? Here is one of the solution, but it does not contain a nested relation. I am specifically looking for solving a nested relationship with Room.

You can use #Relation annotation for POJO classes.
A convenience annotation which can be used in a Pojo to automatically
fetch relation entities. When the Pojo is returned from a query, all
of its relations are also fetched by Room.
Here is the documentation.
Also, this answer has more details: Android Persistence room: "Cannot figure out how to read this field from a cursor"

Related

how to create data class with this dynamic json keys while using retrofit and andorid jetpack libraries

this is json output:
{"query":{"apikey":"...","base_currency":"USD","timestamp":1635972203},"data":{"JPY":113.99127,"CNY":6.39464,"CHF":0.9114,"CAD":1.23881,"MXN":20.54423,"INR":74.44808,"BRL":5.57063,"RUB":71.80098,"KRW":1175.11443,"IDR":14295.1734,"TRY":9.63691,"SAR":3.75119,"SEK":8.52554,"NGN":410.22181,"PLN":3.94541,"ARS":99.81213,"NOK":8.49529,"TWD":27.78459,"IRR":42000.64577,"AED":3.67284,"COP":3827.77643,"THB":33.32047,"ZAR":15.23269,"DKK":6.40357,"MYR":4.15212,"SGD":1.34783,"ILS":3.11624,"HKD":7.78416,"EGP":15.7003,"PHP":50.65881,"CLP":811.73282,"PKR":169.4547,"IQD":1458.01958,"DZD":136.722,"KZT":428.93534,"QAR":3.6499,"CZK":21.94293,"PEN":4.0008,"RON":4.25921,"VND":22747.41599,"BDT":85.57148,"HUF":308.78687,"UAH":26.25062,"AOA":598.0065,"MAD":9.06226,"OMR":0.38491,"CUC":24.00026,"BYR":2.00003,"AZN":1.69502,"LKR":200.00259,"SDG":438.90856,"SYP":2511.07513,"MMK":1746.02836,"DOP":56.29093,"UZS":10690.31508,"KES":111.25137,"GTQ":7.73108,"URY":44.18107,"HRV":6.47553,"MOP":8.01811,"ETB":47.31305,"CRC":635.74442,"TZS":2298.03956,"TMT":3.49009,"TND":2.80635,"PAB":1.00002,"LBP":1505.5263,"RSD":101.16202,"LYD":4.54568,"GHS":6.00013,"YER":249.956,"BOB":6.82018,"BHD":0.377,"CDF":1999.22628,"PYG":6875.19435,"UGX":3550.05822,"SVC":8.7497,"TTD":6.74137,"AFN":90.84208,"NPR":119.13277,"HNL":24.06657,"BIH":1.68483,"BND":1.34753,"ISK":129.16264,"KHR":4060.117,"GEL":3.14003,"MZN":63.22108,"BWP":11.45513,"PGK":3.5113,"JMD":153.22216,"XAF":564.86281,"NAD":15.2189,"ALL":105.53113,"SSP":391.0052,"MUR":42.90097,"MNT":2830.04693,"NIO":35.21094,"LAK":10330.27262,"MKD":53.08156,"AMD":474.80501,"MGA":3928.06091,"XPF":102.48118,"TJS":11.26034,"HTG":98.0013,"BSD":1.00003,"MDL":17.41883,"RWF":1018.02194,"KGS":84.77099,"GNF":9510.20822,"SRD":21.40242,"SLL":10779.18736,"XOF":568.81159,"MWK":807.36713,"FJD":2.06806,"ERN":15.05028,"SZL":15.21372,"GYD":207.78611,"BIF":1980.25293,"KYD":0.82002,"MVR":15.42042,"LSL":15.23032,"LRD":146.80405,"CVE":94.95278,"DJF":177.50237,"SCR":14.42749,"SOS":575.00647,"GMD":52.15123,"KMF":424.6543,"STD":21.11031,"XRP":0.83002,"AUD":1.34372,"BGN":1.68394,"BTC":0.0159,"JOD":0.70801,"GBP":0.73402,"ETH":0.00022,"EUR":0.86112,"LTC":0,"NZD":1.40184}}
The data section contains many key value pairs, but their number and names vary according to base_currency. (For example if i send request with USD there is no USD key or if i send request with CNY there is no CNY key in data section)
So what kind of data class should I create so that I can use it with the retrofit and gsoncreator libraries. (I am also trying to use and learn jetpack android libraries if this is important)
i use that data classes: (I am not using gson annotations because i believe my variables names are correct and i try that it doesn't help)
data class ResponseFromApi(val data: Data,val query: Query)
data class Query(val apikey: String, val base_currency: String, val timestamp: Int)
data class Data(val hashmapForData: HashMap<String, Double>) (i suspect some values are integer but i am not sure)
it doesn't work. Maybe that's not the problem. I don't know but least i need to know, Are these classes correct? What is the proper way to do this.
And i don't know how to get error message from retrofit object so i can identify the problem. But this is another question.
Arpit Shukla's answer is correct.
ResponseFromApi(val query: Query, val data: Map<String,Double>)
It can deserialize key-value map to Map struct.

android Room with kotlin value class?

I'm trying to use a room entity with a value class:
#JvmInline
value class UserToken(val token: String)
and the entity:
#Entity(tableName = TABLE_AUTH_TOKEN)
data class TokenEntity(
#PrimaryKey val id: Int = 0,
val token: UserToken
)
I get the following error:
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 TokenEntity {
^
is it even possible to use room with value class? I couldn't find anything about this. thanks
See the comment from #CommonsWare. Android does not yet support value classes for Room.
The same holds true for the value classes introduced in kotlin 1.5. The type is not supported.
— Support Inline class in Room entity
Here is a possible explanation according to Kotlin Inline Classes in an Android World.
Looking to solve this you could try and add a TypeConverter for your Inline class, but since your Inline class is just the value it wraps when it’s compiled, this doesn’t make much sense and it doesn’t work as you’d expect even if you tried...
I’m just guessing it’s because this is a TypeConverter converting UserId to Int which is basically the same as Int to Int 😭. Someone will probably solve this problem, but if you have to create a TypeConverter for your Inline class then you are still plus one class for the count (multidex). 👎
I think yes if you can provide a type converter for it to change it to some sort of primitive data type (int , string, long ...etc) when it needs to be stored, and to change it back to its class type when it's fetched from database.
You can read about Type Converters from here
Referencing complex data using Room
other than that, your other class should be an entity and bind both your entities together using a Relation.
at least that's what I know about how to use Room.
UserToken always will have only one attribute? In this case, you don't need two classes, just use token: String directly on your entity class;
If you really need keep this class, you have two options:
TypeConverter, where you basically will convert the object into a json, and save as string in the database;
Relation, where you will transform the UserToken in a entity, and on TokenEntity save the tokenId.

How to skip JSON properties in Moshi?

I'm trying to implement JSON parsing in my Android application written in Kotlin using com.squareup.moshi (v1.10.0).
Within the JSON file there are some properties that are not interesting in my case. Let's say, I only need the position to be able to mark the place on a map and the JSON looks like this:
"location":{
"address":{
"country":"..."
},
"position":{
"lat":47.469866,
"lon":19.062435
}
}
If I'm right, the data class in Kotlin should look like this if I'd like to parse that JSON:
#Parcelize
data class Location(
val address: Address,
val position: Position
): Parcelable
#Parcelize
data class Address(
val country: String
): Parcelable
#Parcelize
data class Position(
val lat: Double,
val lon: Double
): Parcelable
In Moshi's documentation I could find the transient keyword to skip values which in Kotlin works as an annotation (#Transient). As the documentation says:
Transient fields are omitted when writing JSON. When reading JSON, the field is skipped even if the JSON contains a value for the field. Instead it will get a default value.
Does it mean that if I don't want to have the address object, I should use the following code?
#Parcelize
data class Location(
#Transient val address: Address? = null,
val position: Position
): Parcelable
Also, what about in general terms? What if I have huge list of properties within a JSON object but I know I only need the 'position' object? Do I still have to create null values to parse the JSON file field-by-field?
I think you are looking for something similar to GSON's #Expose annotations, wherein all model fields are excluded from parsing except those annotated.
This functionality is currently not available in Moshi, so your current implementation using the #Transient annotation seems to be the most optimal solution. (See Moshi issues conversation here.)
Extra food for thought:
You may also wish to use #IgnoredOnParcel on your transient fields since you are implementing the parcelable interface. (Have a look here for some implementation pointers.)
Alternatively you could separate your data model into 2 models - one for use in your app and one which reflects the server (JSON) schema (just as you have done above). The main data model for your app (which could implement parcelable) would contain only the fields you use (for example, the position field). When you parse your data, you then convert that data to your primary data model using some simple adapter. (This is often good practice anyhow, since server-side schemas are inherent to change. This way, any changes in the JSON schema wouldn't end having any ripple effect throughout your code.)
https://github.com/square/moshi#omit-fields-with-transient
Omit fields with transient
Some models declare fields that shouldn’t be included in JSON. For example, suppose our blackjack hand has a total field with the sum of the cards:
public final class BlackjackHand {
private int total;
...
}
By default, all fields are emitted when encoding JSON, and all fields are accepted when decoding JSON. Prevent a field from being included by adding Java’s transient keyword:
public final class BlackjackHand {
private transient int total;
...
}
Transient fields are omitted when writing JSON. When reading JSON, the field is skipped even if the JSON contains a value for the field. Instead it will get a default value.

room database with list inside an object

Till this time I was only doing sql queries which had only simple types or objects where annotation #Embedded was doing a job. Currently I have to extend my sql query to receive "simple data" in the list inside.
data class SimpleDataClass(
#Embedded val myObject: MyObject,
val id: UUID,
val listOfNumbers: List<MyNumbers>)
where
data class MyNumbers( val number: Int)
How to do this? By sql statement? Somehow with code? In DAO I return a:
LiveData<List<SimpleDataClass>>
When I try to receive data as before I get:
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). - java.util.List
You provide very few information about your problem, but I think that Room relations is what you need. See this article Database relations with Room, should be helpful.

Android Room recursive relation

It's possible for room to have Recursive relations?
I have an Entity that can be nested in a Parent/Childs structures something like this
Category1
|_________Category2
|_________Category3
| |_________Category4
|_________Category5`
I'm copying this structure from a json that is obtained from a WebService.
This is my current Entity:
#Entity(tableName = "categories")
public class Category
{
#PrimaryKey
#NonNull
private String code;
private String name;
private String parentCode;
#Relation(parentColumn = "code", entityColumn = "parentCode", entity = Category.class)
private List<Category> childrens;
}
But during compiling I obtain a StackOverflow error:
Cause: java.lang.StackOverflowError
at android.arch.persistence.room.processor.PojoProcessor.doProcess(PojoProcessor.kt:113)
at android.arch.persistence.room.processor.PojoProcessor.access$doProcess(PojoProcessor.kt:74)
at android.arch.persistence.room.processor.PojoProcessor$process$1.invoke(PojoProcessor.kt:105)
at android.arch.persistence.room.processor.PojoProcessor$process$1.invoke(PojoProcessor.kt:74)
at android.arch.persistence.room.processor.cache.Cache$Bucket.get(Cache.kt:46)
I know that I can remove the children from the Entity and iterate the json to save every Category without childrens, and later get the children by the parent code in a separate query, but I just want to know if it is possible to have a recursive Relation like the one in the code
Short Answer: NO you can't have recursive relations.
Long Answer:
Reading the Docs about Relation in Room https://developer.android.com/reference/android/arch/persistence/room/Relation I found that you can't use #Relation annotation inside a class annotated as #Entity
Note that #Relation annotation can be used only in Pojo classes, an Entity class cannot have relations. This is a design decision to avoid common pitfalls in Entity setups. You can read more about it in the main Room documentation. When loading data, you can simply work around this limitation by creating Pojo classes that extend the Entity.

Categories

Resources