问题
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:
Handle
onCreate
,onDowngrade
andonUpgrade
methods in the implementation of yourSQLiteOpenHelper
class.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