Is It Possible To Migrate From Realm To Sqlite?

你。 提交于 2019-12-24 02:41:58

问题


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 must continue using Realm, or users will lost their data completely.

Any idea will be appreciated


回答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.




回答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



来源:https://stackoverflow.com/questions/44208967/is-it-possible-to-migrate-from-realm-to-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!