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);
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 anyone used dynamic realm in android.I'm searching a lot in net but I cannot find any sample code using dynamic realm database, except this realm documentation in android
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
DynamicRealm realm1 = DynamicRealm.getInstance(realmConfig);
// In a DynamicRealm all objects are DynamicRealmObjects
DynamicRealmObject person = realm1.createObject("Person");
// All fields are accessed using strings
String name = person.getString("name");
int age = person.getInt("age");
// An underlying schema still exists, so accessing a field that does not exist
// will throw an exception
person.getString("I don't exist");
// Queries still work normally
RealmResults<DynamicRealmObject> persons = realm1.where("Person")
.equalTo("name", "John")
.findAll();
but when I run this code I'm getting this error message.
The class class_Person doesn't exist in this Realm.
can anyone help me about this and show me a code sample?
A DynamicRealm does not automatically generate any schemas, so if you have a Person model class, you cannot access it dynamically before you opened a Realm normally using Realm.getInstance() (as that will create the schema). Otherwise you manually have to create the Person class using DynamicRealm.getSchema()
in order to create new instance of the existing schema in the realmDB
you have to create an instance by calling realm method createObject inside realm method beginTransaction and commitTransaction like mentioned below.
realm.beginTransaction();
DynamicRealmObject person = realm.createObject("Person");
person.setString("name", "kiran");
person.setInt("age", 29);
realm.commitTransaction();
and the methods setString and setInt are the methods of the DynamicRealmObject.
Please go through the documentation https://realm.io/docs/java/latest/api/io/realm/DynamicRealm.html for more understanding of the DynamicRealms.
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 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.