Realm migration on Android - add new table - android

Is it possible to add a new table without specifying every field "manually" like this:
schema.create("LoyaltyActivity")
.addField("id", String::class.java, FieldAttribute.PRIMARY_KEY)
.addField("source", String::class.java)
.addField("date", String::class.java)
.addField("points", String::class.java)
.addField("reference", String::class.java
Realm class looks like this:
open class LoyaltyActivity(
#PrimaryKey var id: String? = null,
var source: String? = null,
var date: String? = null,
var points: String? = null,
var reference: String? = null
) : RealmObject()
On iOS, it is possible with method provided in SDK: migration.create(typeName: String, value: <Any>)

Related

Firebase RealtimeDatabase retrieve snapshot object exception

This is my model class
#Parcel
data class ClientModel(
var name: String? = "",
var phone: String? = "",
var princpalAddresse: String? = "",
var homeAddresse: String? = "",
var travailleAddresse: String? = "",
var email: String? = "",
var userToken: String? = "",
var principalAddresseCoords: Pair<Double, Double>? = null,
var homeAddresseCoords: Pair<Double, Double>?= null,
var workAddresseCoords: Pair<Double, Double>? = null,
)
My proGuard file keep the class :
-keep class com.olivier.oplivre.models.ClientModel
But! when I try to get the snapshot with a singleValueEventListener I got exception because of the Pair<Double,Double> variables
val utilisationInfo = snapshot.getValue(ClientModel::class.java) //todo CRASH
Exception :
com.google.firebase.database.DatabaseException: Class kotlin.Pair does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
Database Structure :
I think firebase Realtime database treat your principalAddresseCoords as a list of long so in your ClientModel change the value of principalAddresseCoords to emptyList() and the type List
As #Sami Shorman said , firebase took my Pair instance and transform it but not as list, as Hashmap<,> ! so I changed my class model like that :
var principalAddresseCoords: HashMap<String,Double>? = null,
var homeAddresseCoords: HashMap<String,Double >? = null,
var workAddresseCoords: HashMap<String,Double >? = null,
To put the data as Hashmap I just had to do :
clientModel.workAddresseCoords = HashMap<String,Double>().apply {
put("lat",lat)
put("long",long)
}

MutableList<Any> addAll kotlin entire arraylist added as object

Following is my code
var items : MutableList<Any> = arrayListOf()
items.add(TeacherDetails(it?.photo,it?.firstName,it?.lastName,it?.level))
items.add(TeacherBio(it?.bio))
items.add(TitleAccreditations(getString(R.string.acreditations)))
items.add(SessionsTitle(it?.firstName + getString(R.string.apostrophe) + getString(
R.string.sessions)))
items.addAll(listOf(it?.classes ?: arrayListOf()))
items.add(IntroVideo(it?.introVideo))
items.addAll(it?.teachingAccreditations?.split("\n")?.map { Accreditation(
it
) }?: emptyList())
Issue is at following line it is adding entire list as object instead of individual item.
items.addAll(listOf(it?.classes ?: arrayListOf()))
Following is my model
data class Teacher(
#field:SerializedName("firstName")
val firstName: String? = null,
#field:SerializedName("lastName")
val lastName: String? = null,
#field:SerializedName("teacherId")
val teacherId: String? = null,
#field:SerializedName("introVideo")
val introVideo: String? = null,
#field:SerializedName("level")
val level: String? = null,
#field:SerializedName("teachingAccreditations")
val teachingAccreditations: String? = null,
#field:SerializedName("classes")
val classes: List<ClassesItem?>? = null,
#field:SerializedName("photo")
val photo: String? = null,
#field:SerializedName("bio")
val bio: String? = null
)
I guess because your type of list is Any, it will consider adding list instance also as an object, so instead it as listOf() just add directly
items.addAll(it?.classes?.filterNotNull()?: arrayListOf())

Creating field ID Primary Key auto increment in Room

How do i declare an id with primary key auto increment?
package com.ncf.globofly.models
data class Destination(
var id: Int = 0, #PRIMAY KEY Auto Increment
var Sequence: String? = null,
var Description: String? = null,
var Status: String? = null
)
It can be done using annotation first add the dependencies for database and annotation processor after that
data class Destination(
#PrimaryKey(autoGenerate = true)
var id: Int = 0,
var Sequence: String? = null,
var Description: String? = null,
var Status: String? = null
)

Can't perform a search on a DatabaseView using Android Room Persistence LIbrary

Hi Android Developers,
I am trying to use the new version of Android Room to create a DatabaseView and perform a search on the DBView, but the DAO cannot find any results, even when I type pretty obvious queries.
Here is the version of my Room Gradle dependencies.
//Android Room
def room_version = "2.1.0-alpha04"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Here is my DatabaseView
#DatabaseView("SELECT * from popular UNION SELECT * from trending UNION SELECT * from favorite",
viewName = "searchView")
data class SeriesEntity(
#PrimaryKey
var id: Int? = null,
var originalName: String? = null,
var name: String? = null,
var popularity: Double? = null,
var voteCount: Int? = null,
var voteAverage: Double? = null,
var firstAirDate: String? = null,
var posterPath: String? = null,
var genreIds: List<Int>? = null,
var originalLanguage: String? = null,
var backdropPath: String? = null,
var overview: String? = null,
var originCountry: List<String>? = null
)
Here is my DAO:
#Dao
interface SearchSeriesDao {
#Query("""
SELECT * FROM searchView WHERE name MATCH :query """)
fun query(query : String) : LiveData<List<SeriesEntity>>
}
My main Database class:
#Database(entities =
arrayOf(
FavoritesEntity::class,
PopularEntity::class,
TrendingEntity::class),
views = arrayOf(SeriesEntity::class), version = 5)
Every time that I try to use my DAO to search for a simple record in my view, the LiveData returned is empty.

Customise single data class without creating multiple data classes in Kotlin

I have a scenario. I have created a data class in Kotlin like this:
data class AgentDetails(
val mobileNo: String,
val Name: String,
val Email: String,
val Password: String,
val Occupation: String,
val pincode: String,
val usertype: String,
val profilepic: String,
val AccountStatus: String
)
I want to send different type of objects of this data class to a web service:
1st object example:
val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString(),
Name = userNameText.text.toString(),
Email = emailIdText.text.toString(),
Password = HashUtils.sha1(passwordText.text.toString()),
Occupation = item,
pincode = pinCodeText.text.toString(),
usertype = "Agent",
profilepic = "null", AccountStatus = "pending")
In 2nd object I only want to send mobile number. I dont wanna include any other field. Something like this:
val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString())
And in 3rd object I only wanna send email id. Instead of creating multiple data classes. Can I use the same data class for multiple implementations?
Personally, I'd define three objects because they represent three different concepts (or projections of a concept). But if you make your properties nullable and provide a default value of null, you can get away with creating them as you want...
data class AgentDetails(
val mobileNo: String? = null,
val name: String? = null,
val email: String? = null,
val password: String? = null,
val occupation: String? = null,
val pincode: String? = null,
val usertype: String? = null,
val profilepic: String? = null,
val accountStatus: String? = null
)
Note: I've changed some of your property names to camelCase, as is the proper convention. And these all work fine:
AgentDetails(mobileNo = mobileNumberText.text.toString())
AgentDetails(email = "foo#example.com")
AgentDetails(name = "Foo", password = "Bar")
All of the other fields not provided will be null, and the types will be nullable, so you'll have to guard against that. Otherwise, I'd define three data classes for this.
Another solution would be to consider a sealed class structure:
sealed class AgentDetails
data class AgentByName(val name: String) : AgentDetails()
data class AgentByEmail(val email: String): AgentDetails()
// etc..
And then use it in a when expression:
fun doSomethingWithAgents(agentDetails: AgentDetails) {
when (agentDetails) {
is AgentByName -> // Do something
is AgentByEmail -> // Do Something
}
}
The easiest way is to make the fields nullable and provide default values:
data class AgentDetails(
val mobileNo: String? = null,
val Name: String? = null,
val Email: String? = null,
val Password: String? = null,
val Occupation: String? = null,
val pincode: String? = null,
val usertype: String? = null,
val profilepic: String? = null,
val AccountStatus: String? = null
)

Categories

Resources