我想把一个已经有一些数据的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/