是否可以从领域迁移到Sqlite? [英] Is It Possible To Migrate From Realm To Sqlite?

查看:66
本文介绍了是否可以从领域迁移到Sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因(无论如何,即使是ABI拆分,apk的大小也很大),我需要完全删除Realm并使用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.

我找不到办法.看来该应用程序必须继续使用Realm,否则用户将完全丢失其数据.

I couldn't find a way. It seems the app must continue using Realm, or users will lost their data completely.

任何想法都会受到赞赏

推荐答案

我认为我不完全了解您的问题所在.如果您要问是否有一种工具可以为您自动进行数据迁移,那么没有,没有这样的工具.

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.

否则,这很简单:

  1. 在实现SQLiteOpenHelper类的过程中处理onCreateonDowngradeonUpgrade方法.

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

在创建表之后,在onCreate方法中,从 Realm 获取所有数据,然后将其插入SQLite表.

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

类似这样的东西:

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

我实际上建议您研究如何减小APK大小在使用Realm时,但由您自己决定

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

编辑1 您必须确保先迁移数据,然后再删除Realm文件.虽然不是使APK变大的数据文件,而是Realm附带的实际库.因此,为此,很遗憾,您将必须执行两个步骤:首先将数据迁移到SQLite的发行更新,再经过一段合理的时间(例如一周),您可以发行将Realm库完全淘汰的更新.希望有道理.

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.

这篇关于是否可以从领域迁移到Sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆