Reputation: 1517
Today I'm switching to Realm and I was wondering about the exact purpose of RealmConfiguration.
Let's assume that I have two activities which make use of 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? What does it do exactly? is this my data? Should I declare this once in the application class for instance?
// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);
I tried to search on Realm, but couldn't find an answer.
Thanks a lot for your assistance,
Upvotes: 2
Views: 2520
Reputation: 43364
When getting the defaultInstance of Realm, I have to specify the RealmConfiguration. Do I have to call this each time on my two activities?
You can, but you shouldn't.
Should I declare this once in the application class for instance?
Yes.
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
Then you can just say this anywhere in your app.
Realm realm = Realm.getDefaultInstance();
It will be configured with the RealmConfiguration you set in the application class.
What does it do exactly? is this my data?
It is not your data, it is the configuration for your data. E.g. what the name of the database file is, what version the schema is, whether or not the data is encrypted, how migrations to new schema versions should be handled, etc. See more options here.
If you have multiple Realm files in your app, you can use multiple RealmConfigurations. One for each. It's perfectly normal to only have one Realm file across your app though.
Upvotes: 11