今天我要切换到 Realm,我想知道 RealmConfiguration 的确切用途。
假设我有两个使用 Realm 的 Activity 。
在获取 Realm 的 defaultInstance 时,我必须指定 RealmConfiguration。我每次都必须在我的两个 Activity 中调用它吗?它具体做什么?这是我的数据吗?例如,我应该在应用程序类中声明一次吗?
// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);
我试图在 Realm 上搜索,但找不到答案。
非常感谢您的帮助,
最佳答案
When getting the defaultInstance of Realm, I have to specify the RealmConfiguration. Do I have to call this each time on my two activities?
可以,但不应该。
Should I declare this once in the application class for instance?
是的。
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
然后您就可以在您的应用中的任何地方说这句话。
Realm realm = Realm.getDefaultInstance();
它将使用您在应用程序类中设置的 RealmConfiguration 进行配置。
What does it do exactly? is this my data?
这不是您的数据,而是您数据的配置。例如。数据库文件的名称是什么,架构是什么版本,数据是否加密,应如何处理迁移到新架构版本等。查看更多选项 here .
如果您的应用中有多个 Realm 文件,您可以使用多个 RealmConfigurations。每人一个。不过,在您的应用中只有一个 Realm 文件是完全正常的。
关于Android - Realm - 正确使用Realmconfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432421/