Android jetpack room use problem summary

Android jetpack room use problem summary

问题一:Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false.

The pattern export directory is not provided to the annotation processor, so we cannot export patterns. You can provide the "room.schemaLocation" annotation processor parameter, or you can set exportSchema to false.

Solution

1. Add the following configuration in the app's build.gradle:

      javaCompileOptions {
    
    
            annotationProcessorOptions {
    
    
                arguments = ["room.schemaLocation":
                                     "$projectDir/schemas".toString()]
            }
        }

2. Add exportSchema = false in the database annotation

@Database(entities = {
    
    User.class},version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    
    
    public abstract UserDao userDao();

}

the reason:

When compiling, Room will export the database schema information as a JSON file (the default exportSchema = true export schema). To export the schema, set the room.schemaLocation annotation processor property in the build.gradle file (set the location where the json is stored).

问题二:There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors.

Solution: When the
entity has multiple constructors, you can use the @Ignore annotation to ignore the rest of the constructor and leave only one.

    public User() {
    
    
    }
    @Ignore
    public User(int uid, String firstName, String lastName) {
    
    
        this.uid = uid;
        this.firstName = firstName;
        this.lastName = lastName;
    }

Note: You can’t ignore all the constructors, either none of the constructors or the other is ignored.

Guess you like

Origin blog.csdn.net/guojingbu/article/details/115245007