Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

java - Android Room property validate before saving to the database?

I have a entity as follows

@Entity
data class User(
    @PrimaryKey 
    var id: String,

    var firstName: String,

    var lastName: String
) 

Lets say the accepted firstName and lastName are strings which should be strictly ASCII, How can I create a validation before saving to database, or do some pre processing when saving the data ?

The question is about having a validator to validate the data not just an ASCII check.

question from:https://stackoverflow.com/questions/65867499/android-room-property-validate-before-saving-to-the-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can make the Dao as an abstract class, the actual saving is where you run the query, but you call it from another method.

@Dao
public abstract class UserDao {
    public void insert(User user){
       if(yourCondition){
         insertInternal(user);
       }
    }
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertInternal(User user);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...