I am trying to add a new property to the Realm object UserDetails. Here is my try:
class CustomerDetails: Object {
dynamic var customer_id = 0
dynamic var customer_name = ""
}
Now i need to add a new property "company_name" to the object UserDetails which is already being created earlier. How to add a new one to the existing Realm object?
To upsert an object, call Realm. create() with the update mode set to modified . The operation either inserts a new object with the given primary key or updates an existing object that already has that primary key.
Adding Data Into RealmCreate an object that will be inserted in the database by using r. createObject(ObjectClass, PrimaryKey) to which we pass our model class and the primary key. With the object that we have created, we can add data by accessing its attribute.
You can either upload the image to an open-source image uploader, like Imgur, and then download it back to cache it, or you can store the image locally in your Realm database. The latter has the advantage that it does not require Internet connectivity.
An object schema is a configuration object that defines the fields and relationships of a Realm object type. Android Realm applications define object schemas with Java or Kotlin classes using Realm Schemas.
Two ways to do it:
Just delete your app from simulator and run it again. Everytime you change properties on your Realm objects your existing database becomes incompatible to the new one. As long as you are still in the developing stage you can simply delete the app from the simulator / device and start it again.
Write this code in AppDelegate's disFinishLaunchWithOptions method:
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
Realm.Configuration.defaultConfiguration = config
let realm = try! Realm()
I suggest you to follow the second one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With