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);
Related
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);
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.
I am adding some data into realm database after every 15 sec through a service.
After a whole night, the size of app become 350mb due to realm, its confirm..
But if i delete that data from realm, after some condition, the data deleted but the size of app still shown 350mb.
The question is why the app size is not shrinking now.
Realm currently doesn't automatically reclaim the space used by your database, but it will be reused if you later add data again.
If you wish to free the disk space you can use Realm.compactRealm(): https://realm.io/docs/java/latest/api/io/realm/Realm.html#compactRealm-io.realm.RealmConfiguration-
However, a file size of 350 MB sounds like quite a lot. Are you really inserting that much data?
Realm realm = null;
RealmConfiguration config = new RealmConfiguration.Builder(MyApplication.appInstance().getApplicationContext())
.deleteRealmIfMigrationNeeded()
.build();
try {
realm = Realm.getInstance(config);
} catch (Exception e) {
}
RealmResults<MqttLocationModel> result2 = realm.where(MqttLocationModel.class).equalTo("uid", objectId).findAll();
Log.e("Delted object id",""+objectId);
Log.e("Delted DB Size",""+result2.size());
realm.beginTransaction();
result2.clear();
realm.commitTransaction();
RealmResults<MqttLocationModel> result3 = realm.where(MqttLocationModel.class).equalTo("uid", objectId).findAll();
Log.e("Delted final DB Size",""+result3.size());
realm.close();
realm.compactRealm(config);
Your service executes on a background thread, so you probably have a single realm open on a BACKGROUND THREAD for the whole duration of the sync procedure, thus creating multiple versions of the Realm.
If this is true, you should either close and reopen the Realm after a transaction, or use waitForChange ().
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.
This question already has answers here:
"realm migration needed", exception in android while retrieving values from realm db
(5 answers)
Closed 5 years ago.
Whenever I change the model like adding more fields, the app crash with io.realm.exceptions.RealmMigrationNeededException error. This can only be resolved when I uninstalled and reinstalled the app.
Any suggestion to do migration? I am using only the default instance.
If you don't have any problem in loosing your old data then you can delete Realm Configuration and create new one.
Realm realm = null;
try {
realm = Realm.getInstance(MainActivity.this);
} catch (RealmMigrationNeededException r) {
Realm.deleteRealmFile(MainActivity.this);
realm = Realm.getInstance(MainActivity.this);
}
OR
RealmConfiguration config2 = new RealmConfiguration.Builder(this)
.name("default2")
.schemaVersion(3)
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(config2);
you have to do Migration if you don't want to loose your data please see this example here.
You should be able to find the information you need here:
https://realm.io/docs/java/latest/#migrations
Just changing your code to the new definition will work fine, if you
have no data stored on disk under the old database schema. But if you
do, there will be a mismatch between what Realm sees defined in code &
the data Realm sees on disk, so an exception will be thrown.
Realm migrations in 0.84.2 are changed quite a bit, the key points on making a realm (0.84.2) migration work for me were understanding that:
The schemaVersion is always 0 when your app has a realm db without
specifying the schemaVersion. Which is true in most cases since you
probably start using the schemaVersion in the configuration once you
need migrations & are already running a live release of your app.
The schemaVersion is automatically stored and when a fresh install of your app occurs and you are already on schemaVersion 3, realm
automatically checks if there are exceptions, if not it sets the
schemaVersion to 3 so your migrations aren't run when not needed.
This also meens you don't have to store anything anymore in
SharedPreferences.
In the migration you have to set all values of new columns when the type is not nullable, ...
Empty Strings can be inserted but only when setting convertColumnToNullable on the column