I started working on the logic for my migration, using this code:
https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/MigrationExampleActivity.java
And after writing the code, I get an error at this line:
String path3 = MigrationClass.copyBundledRealmFile(this, this.getResources().openRawResource(R.raw.default1), "default1");
It can't find the R.raw.default1 file, because until now, I used the default Realm like this:
Realm realm = Realm.getInstance(context);
My question is where can I get the file path for this realm file?
Realm just uses the Context to call getFilesDir() and the default Realm is called default.realm. So in your case you should use:
String realmPath = new File(context.getFilesDir(), "default.realm").getAbsolutePath();
Realm.migrateRealmAtPath(realmPath, new CustomMigration());
You can get the path of your realm file by calling the "getPath()" method:
Here an Example:
realm.getPath()
Related
I have a Realm DB file, with name "abc.realm". How to change this name to something else? Should I just replace the file name using IO operations or can I do it with migrations? Not able to find any satisfactory answer neither on the web nor on StackOverflow.
Realm stores 2 files, the realm itself and a .lock file. So if you call your realm "abc.realm", then next to this file there is also "abc.realm.lock".
The way to go about renaming your realm file is,
Make sure you find the location of both files
Rename both files with the same name but keeping the ".lock" extension on the lock file
Modify the path to the realm that you pass to the RealmConfigurationBase inheritor
Clearly before doing any of this, make sure to backup your database, just in case.
I don't know what programming language you're writing your android application in, so I'll go with a skeleton in pseudocode
private void BackupRealmFile(string realmLocation, string saveLocation)
{
// make a copy of the file and store it somewhere
}
void YourMainMethod()
{
BackupRealmFile("some/path", "your/backup/path");
IOLib.RenameFile("some/path/abc.realm", "some/path/newName.realm");
IOLib.RenameFile("some/path/abc.realm.lock", "some/path/newName.realm.lock");
var config = new RealmConfiguration("some/path/newName.realm");
// maybe some more settings on your conf
var realm = Realm.GetInstance(config);
}
I hope this helps.
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.
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.