[JAVA] ActiveAndroid => ReActiveAndroid migration

When I tried to release the app and set targetSdkVersion to 26, Active Android did not work. Apparently it doesn't work properly because it is not maintained. https://www.tsurutan.com/entry/2018/10/08/Migration_ActiveAndroid_to_Room https://qiita.com/pside/items/03e6256b404c75f5974b#activeandroid%E3%81%AE%E7%A7%BB%E8%A1%8C%E3%83%91%E3%82%B9%E3%81%A8%E3%81%97%E3%81%A6%E3%81%AEreactiveandroid

There is a fair amount of existing description, and it was hard to move to a completely new library, so it is quite compatible. Migrate to ReActive Android (If it is new, there is more choice. Room, etc ...)

Below is a memo of correspondence at that time I don't know if there is any demand, but if the same thing helps people who are in trouble ...

Correspondence

Corresponds according to the following document https://imangazalievm.gitbooks.io/reactiveandroid/migration-from-activeandroid.html

The DB name and table name are rewritten as DbName and TableName, respectively.

--in build.gradle

implementation 'com.reactiveandroid:reactiveandroid:1.4.3'
implementation 'android.arch.persistence:db:1.1.1'

Added.

--Create DbName.java and describe@Database (name = "DbName", version = 1)and the following migration method.

DbName.java


static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        // Since we didn't alter the table, there's nothing else to do here.
    }
};

--Call from the initialization part of App.java in the following form

App.java


        DatabaseConfig appDatabaseConfig = new DatabaseConfig.Builder(DbName.class)
                .addMigrations(DbName.MIGRATION_1_2)
                .build();

        ReActiveAndroid.init(new ReActiveConfig.Builder(this)
                .addDatabaseConfigs(appDatabaseConfig)
                .build());

--Delete all import statements of ʻactiveandroid in the source code and import the reactiveandroid. Since the directory structure has not changed, you should be able to replace ʻactiveandroid with reactiveandroid. --Add @PrimaryKey (name =" id ") to the primary key. --Changed Table annotation of each Model class to @Table (name =" tableName ", database = DbName.class) --Changed the syntax of method call as follows --Change new Select () to Select --Changed ʻexecuteSingle to fetchSingle --Changed ʻexecute to fetch

Like this ⬇︎

- new Select().from(TableName.class).where("Id = %d".format(id)).executeSingle[TableName]();
+ Select.from(TableName.class).where("Id = %d".format(id)).fetchSingle();

--- Since the method corresponding to ʻexist () is not found, it is judged by count ()> 0`. (Check it because it may affect the performance. If it seems to come out, add a limit) --GetId () and other getters / setters that are standard equipment on ActiveAndroid do not exist on ReActiveAndroid, so add them yourself if necessary. --Changed transaction description

- ActiveAndroid.beginTransaction();
- ActiveAndroid.setTransactionSuccessful();
- ActiveAndroid.endTransaction();

+ ReActiveAndroid.getDatabase(DbName.class).beginTransaction();
+ ReActiveAndroid.getDatabase(DbName.class).getWritableDatabase().setTransactionSuccessful();
+ ReActiveAndroid.getDatabase(DbName.class).endTransaction();

If you do so far, it should work ...

Memo where I got caught

If you follow the above procedure, it should not come out.

Please provide a Migration

Please provide a Migration in the builder or call disableMigrationsChecking in the builder in which case ReActiveAndroid will re-create all of the tables.

Caused by forgetting to write the migration process

Add and call the migration method according to the above procedure to eliminate it

SupportSQLiteDatabase is not resolved

https://mvnrepository.com/artifact/android.arch.persistence/db Check the version with

implementation 'android.arch.persistence.db:1.1.1'

To gradle >Error:Failed to resolve: android.arch.persistence.db:1.1.1: Error. >http://fengwanxingzhou.coding.me/Android%E9%94%99%E8%AF%AF/ It seems that the package name is specified incorrectly. ʻAndroid.arch.persistence: db: 1.1.1` and it passed.

Couldn't read row 0, col -1 from CursorWindow

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

In ActiveAndroid, it can be described without using Cursor, so it should be used in some internal processing.

Looking at the internal processing of the error part (Select), I can't find the place where cursor is used ... It was used inside fetchSingle (where the definition of Table is brought). When I put a breakpoint here, I was trying to bring in a column where ʻidNamewas" _id ". The cause was that I brought the template as it was and set it as @PrimaryKey (name =" _ id "). (The real column name is ʻId) So

TableName.java


- @PrimaryKey(name = "_id")
+ @PrimaryKey(name = "Id")

Corrected and resolved.

Recommended Posts

ActiveAndroid => ReActiveAndroid migration
Rails migration