Skip to content Skip to sidebar Skip to footer

Is It Possible To Migrate From Realm To Sqlite?

For some reasons(mainly large apk size even with ABI splits, anyway) i need to remove Realm completely and use Sqlite without losing data. I couldn't find a way. It seems the app m

Solution 1:

I don't think I understand fully what your problem is. If you are asking if there is a tool that will automate the data migration for you, then no, there is no such a tool.

Otherwise it is rather straight forward:

  1. Handle onCreate, onDowngrade and onUpgrade methods in the implementation of your SQLiteOpenHelper class.

  2. In your onCreate method, right after you create tables, get all your data from Realm and insert into into SQLite tables.

Something like this:

Realm realm = Realm.getDefaultInstance();
RealmResults<MyClass> all = realm.where(MyClass.class)
                                 .findAll();
for (MyClass instance : all) {
    doInsert(instance);
}

I actually suggest that you look into how to reduce APK size while using Realm, but it is up to you

EDIT 1 You would have to make sure that you migrate the data first and then delete Realm files. Although it is not the data files that make your APK big, but rather the actual libraries that come with Realm. So for this you will, unfortunately, have to take two steps: first release update that migrates the data to SQLite, and after some reasonable time (like a week) you can release update that takes Realm libraries completely out. Hope it makes sense.

Solution 2:

As i know (not sure) if you are using default RealmConfig, Context.getFilesDir() can return the folder of where default.realm is located. You dont need permission to modify them. You can iterate files under this folder to delete them. Hope it works.

PS : Apache-commons has a great FileUtils.java

Good luck

Emre

Post a Comment for "Is It Possible To Migrate From Realm To Sqlite?"