android - 如何使用现有的 Realm 作为资源

标签 android realm

我想把一个已经有一些数据的Realm数据库作为只读资源文件放到Android的assets文件夹中,我该怎么办?

最佳答案

根据 https://realm.io/docs/java/latest/#read-only-realms

您可以将the_realm.realm文件放入/assets文件夹

然后你可以像这样构建一个配置(这也使它成为 readOnly())

RealmConfiguration config = new RealmConfiguration.Builder()
    .assetFile("the_realm.realm")
    .readOnly()
    // It is optional, but recommended to create a module that describes the classes
    // found in your bundled file. Otherwise if your app contains other classes
    // than those found in the file, it will crash when opening the Realm as the
    // schema cannot be updated in read-only mode.
    .modules(new BundledRealmModule())
    .build();

然后 BundledRealmModule 就像

@RealmModule(classes={Dog.class, Cat.class})
public class BundledRealmModule {
}

关于android - 如何使用现有的 Realm 作为资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48588891/

相关文章:

android - 哪个是 HSV 皮肤检测器 openCV android 的颜色空间?

android - Android Studio 中的自定义字体

ios - 如何删除 Realm 中的对象?

swift - Realm 查询和过滤器

android - VideoView可以播放应用数据目录下的mp4文件

php - 如何通过Android获取post参数到PHP后的json数组

iphone - 绘制多个相同对象的最有效方法?

ios - 无法访问 Realm 获取的数据

swift - 如何将 Realm 通知抽象为带有闭包的通用结构?

java - 如何将 RealmResults<Model> 转换为 ArrayList<Model>?