When the Litepal database calls an update, it needs to override the non-ID field in the delegate and determine whether the field is updated by building an empty instance to check that the field is equal to the default value. LitePalSupportException is thrown if the table does not have a default no-argument constructor.

When writing data prototypes using Kotlin, you cannot use the data class, which has no parameterless constructor. You need to change the data class to plain class.

data class BloggerModel( var userId: String, var userName: String, var fullName: String, var tagLabel: String, var introInfo: String, var avatarUrl: String, var url: String, var visitTime:Long=0L, var count:Int=0, var id: Long = 0L):LitePalSupport(),Serializable // change to class BloggerModel() : LitePalSupport(), Serializable { constructor( userId: String, userName: String, fullName: String, tagLabel: String, introInfo: String, avatarUrl: String, url: String, visitTime: Long, count: Int, id: Long ) : this() { this.userId = userId this.userName = userName this.fullName = fullName this.tagLabel = tagLabel this.introInfo = introInfo this.avatarUrl = avatarUrl this.url = url this.visitTime = visitTime this.count = count this.id = id } lateinit var userId: String lateinit var userName: String lateinit var fullName: String lateinit var tagLabel: String lateinit var introInfo: String lateinit var avatarUrl: String lateinit var url: String var visitTime: Long = 0L var count: Int = 0 var id: Long = 0L}Copy the code
The following is the breakpoint tracing process

Update (id);

2, update field:

3. Iterate over non-ID fields one by one:

4. When updating, check if the field is equal to the default value (throw an exception here) or update the field if it is not. Update fields directly when saving without checking:

5, determine whether the field is equal to the default value of the specific code,Comment: Parse incoming fields. Check if this field has a default value. BaseObj requires a default constructor, otherwise LitePalSupportException will be thrown.:

Create an empty instance:

7. NewInstance () :

In summary, when Litepal data is updated, the no-parameter constructor of the table is called, so you need to keep the no-parameter constructor when writing the table.