今天我转而使用Realm,我想知道RealmConfiguration的确切目的。
我们假设我有两个使用Realm的活动。
获取Realm的defaultInstance时,我必须指定RealmConfiguration。我每次参加两项活动都需要打电话吗?它究竟做了什么?这是我的数据吗?我应该在应用程序类中声明一次吗?
// 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,但无法找到答案。
非常感谢您的协助,
答案 0 :(得分:9)
获取Realm的defaultInstance时,我必须指定RealmConfiguration。我每次参加两项活动都需要打电话吗?
你可以,但你不应该。
我应该在应用程序类中声明一次吗?
是
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进行配置。
它究竟做了什么?这是我的数据吗?
这不是您的数据,而是您数据的配置。例如。数据库文件的名称,架构的版本,数据是否加密,应如何处理迁移到新架构版本等。请参阅更多选项here。
如果您的应用中有多个Realm文件,则可以使用多个RealmConfiguration。每个一个。虽然在您的应用中只有一个Realm文件,这是完全正常的。