Is there a way to ask Realm in Android if migration is needed?
I have my configuration in place so that realm gets deleted if migration is needed.
realmConfiguration = new RealmConfiguration
.Builder(context)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);
realm = Realm.getInstance(realmConfiguration);
This is fine for my purpose, but I need to know when this happens, because I need to store default data into the database if it has been erased.
Any tips how I can react to realm deleting the data?
You should use the initialData method to which you can provide an initial transaction that sets up your data - it runs if the Realm is empty, or if deleteIfMigrationNeeded() deleted your Realm.
Related
I have main android project that uses another android module. In main android project am getting real instance with some configuration like as.
realm = Realm.getInstance(someConfig());
Am initing Realm from main app Application class, as follows
Realm.init(Context);
In my module when I try to call following line it shows error.
Realm db = Realm.getDefaultInstance();
Error:
error Wrong key used to decrypt Realm.
W/System.err:
java.lang.IllegalArgumentException: Wrong key used to decrypt Realm.
1.) I genuinely think that a library that relies on having its own RealmConfiguration to be set as the "default configuration" is heavily intrusive. So library code should use Realm.getInstance(configuration).
2.) If you want the configurations to refer to different files, you might want to set a different name using new RealmConfiguration.Builder().name("somename.realm")/*...*/.
Add below code to parent class which extends Application/MutlidexApplication class or where ur intializing Realm:
Realm.init(Parent.this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name(AppConstants.DATABASE_NAME)
.schemaVersion(2)
// .migration(new DBMigration())
// .migration(new Migration())
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);
Realm.getInstance(realmConfiguration);
I am learning to use Realm database, I found. Realm file path can not be modified. Does anyone know why this is, or how to solve it?
You can specify the directory where your Realm is stored using https://realm.io/docs/java/latest/api/io/realm/RealmConfiguration.Builder.html#directory-java.io.File-. For example:
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder()
.directory("store/it/here")
.name("my-realm-file")
.build();
Realm realm = Realm.getInstance(config);
Currently I am using
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
Which is nice while developing, but not ideal when releasing updates to the app store.
I have wrote migration code using RealmMigration so that users do not have their Realm wiped upon update.
But now migrations will become an annoyance while developing if I ever make any changes to the schema.
Is there anyway to have Realm try and use the migration... and if the migration fails just have realm deleted?
You could do something like:
try {
Realm realm = Realm.getInstance(config);
realm.close();
} catch (RealmMigrationNeededException e) {
Realm.deleteRealm(config);
}
Realm realm = Realm.getInstance(config);
I have a Test.realm file inside the asset folder. But I don't know how to load the Realm file inside an activity. I have tried this
RealmConfiguration config = new RealmConfiguration.Builder(this)
.name("Test.realm").build();
Realm realm = Realm.getInstance(config);
RealmResults<RealmTestClass0> results = realm.where(RealmTestClass0.class)
.findAll();
But it was crashing on setting config line (second line). From the log it says
Caused by: io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided
So how is the right way to load Realm file?
Thanks in advance.
Realm.getInstance() is correct method for getting Realm's instance.
It looks that you change some of your Realm objects or add new one. Realm detected it and tells that you have new data schema and have to migrate (RealmMigrationNeededException).
If you are only developing now - delete application and install it again. It should start to work fine. If your application is in production - you should write some migration code (https://realm.io/docs/java/latest/#migrations)
I am trying to create a database for my android application using Realm. I need to have data that is pre-populated when the app is installed. Setting a Realm Migration as part of the RealmConfiguration does not run when the version of the database is 0 (defaults to 0 initially). How can I add data the first time the application is setup?
Realm Java 0.89 introduced a method that allows for specifying a transaction to be run when a Realm database is created for the first time. This method, RealmConfiguration.Builder.initialData(Realm.Transaction transaction), is called as part of setting up the RealmConfiguration Builder.
For example
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("myrealm.realm")
.initialData(new MyInitialDataRealmTransaction()),
.build();
What I am doing right now that works is to check if this is the first time my app is installed and create a new object.
if (Preferences.freshInstall(getApplicationContext())) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
Category inbox = new Category("Inbox", "#445566");
realm.copyToRealm(inbox);
realm.commitTransaction();
Preferences.notNew(getApplicationContext());
}
There should be a better way to do this using Realm Migrations
The initial data transaction setup, as shown by #Benjamin in Realm Java works! I only wish that it was present in Realm Cocoa, as well.
I've created an issue for this, in the Github tracker here, #3877.