How to Change The Data Type in Realm Database

Is there a way to change the Data Type in Realm Database?

I may have misunderstand the question but you appear to have existing data stored as as a String and you want to 'convert' all of those to an Int.

You cannot directly change the type to another type and have the stored data changed as well. If you do, it will be flagged with an error.

Error!
Migration is required due to the following errors:
- Property 'Item.itemid' has been changed from 'string' to 'int'.

You need to incorporate a migration block to 'convert' the string value to an Int. Assuming we add a new Int property to our object `item_id', something along these lines will migrate your strings to int's and in the case where the string is not a valid it, it will be assigned a value of 0

Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.enumerateObjects(ofType: Item.className()) { oldObject, newObject in
let stringValue = oldObject!["itemid"] as! String
newObject!["item_id"] = Int(stringValue) ?? 0
}
}
})

Also, as soon as Realm is accessed, the object models are written to the Realm file. So a simple matter of

let items = realm.object(Item.self)

Will store that model even if no data was ever written. If, after that line, the var type is changed from a String to an Int, it will throw the migration error.

Deleting the Realm and starting from scratch is one option if that's the case, and as mentioned above, a migration block.

If this is brand new model that has never been used, then as the comments and other answer suggest, just change the String to an Int.

Change datatype of Realm field - Java

There isn't an one-liner method, but you can follow the same pattern as in our Migration example: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L132-L132

        schema.get("MyClass")
.addField("new_key", int.class)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setInt("new_key", Integer.valueOf(obj.getString("old_key")));
}
})
.removeField("old_key")
.addPrimaryKey("new_key")
.renameField("new_key", "old_key");

Change Data Type from String to Long in Realm Android using RealmMigration

Below I just mentioned a example for migrating the field type from String to int using the example mentioned in the comment by @christian-melchior

public class DataMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();

......

// Modify your check according to your version update.
if (oldVersion == 1) {
// Below I am going to change the type of field 'id' from 'String' to 'int'

// Get Schema of a Particular Class. Change --Testing-- with the respected pojo class name
RealmObjectSchema schema = schema.get("Testing");

// Add a new temporary field with the new type which you want to migrate. 'id_tmp' is a temporary integer field.
schema.addField("id_tmp", int.class);

// Set the previous value to the new temporary field
schema.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
// Implement the functionality to change the type. Below I just transformed a string type to int type by casting the value. Implement your methodology below.
String id = obj.getString("id");

obj.setInt("id_tmp", Integer.valueOf(id));
}
});

// Remove the existing field
schema.removeField("id");

// Rename the temporary field which hold the transformed value to the field name which you insisted to migrate.
schema.renameField("id_tmp", "id");

oldVersion++;
}

......

}
}

Don't forgot to update the schema version and refer the above migration class instance in the RealmConfiguration instance of realm.

How to update a type of a realm model property in Objective c?

You need to perform a migration so the data in the database fits the new data model. Please check the Migrations section of the Realm documentation.

How to update existing value on realm database swift?

If you literaly want to:

I want to make any row from 'false' to 'true' or vice versa

Here's the code that will make all false rows true, and all true row false.

let toDoResults = realm.objects(TodoData.self)
for toDo in toDoResults {
try! realm.write {
toDo.rows = !toDo.rows
}
}

I think though, you just want to toggle a row when the user makes a change in your tableView.

let selectedTodo = "a"
let results = realm.objects(TodoData.self).filter("todos == %@", selectedTodo)
if let thisTodo = results.first {
try! realm.write {
thisTodo.rows = !thisTodo.rows //will toggle from t to f and from f to t
}
}

Data type for an array of Object in realm - react-native realm

const myScheme = {
name: "myScheme",
primaryKey: "_id",
properties: {
_id: "objectId",
_partition: "string",
name: "string",
tasks: "myData[]"
}
};

const myData = {
name: "myData",
primaryKey: "_id",
properties: {
_id: "objectId",
_partition: "string",
firstname: "string",
lastname: "string",
}

};

Change Values without affecting stored objects - Realm swift

Unfortunately, as you've already stated, you can't apply new values to Realm properties 'temporarily' once the Realm Object has been written to a Realm file. This is an established fact of Realm's implementation, and there's no official way around it. As such, it'll be necessary to come up with a different mechanism to store those temporary values elsewhere, and write them to Realm when the time comes.

Something you could consider here is using Realm's ignored properties feature. Essentially, you can mark certain properties in a Realm Object to explicitly NOT get written to Realm files, and you're free to modify them anytime (and on any thread) you want.

class TestObject: Object {
dynamic var tmpValue = ""
dynamic var actualValue = ""

override static func ignoredProperties() -> [String] {
return ["tmpValue"]
}
}

If the type of temporary data getting generated is consistently the same type each time, you can create special ignored properties in your model object to hold onto each of those properties, and then when you want to actually save them in Realm, you just copy the values from the ignored properties to the Realm properties in a write transaction.

Of course, these ignored values only persist in the instance of the Realm Object in which they were added. So if your architecture means that you're dealing with multiple Realm Object instances pointing to the same data source on disk, it might be better to separate these temporary values completely from the Realm Object and hang onto them in memory somewhere else.

Finally, while you've said you'd rather not create non-persisted copies of Realm objects because of their size, a few people have already created some pretty cool ways to perform such a copy without much code (My favourite example is in the Realm-JSON project), which still might be worth considering. Good luck!



Related Topics



Leave a reply



Submit