Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new property to a Realm Object?

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?

like image 333
Shibili k.p Avatar asked Oct 07 '17 08:10

Shibili k.p


People also ask

How do you update Realm objects in react native?

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.

How do I add data to my Realm database?

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.

Can we store image in Realm?

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.

What is Realm schema?

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.


1 Answers

Two ways to do it:

  1. 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.

  2. 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.

like image 85
Shibili k.p Avatar answered Sep 19 '22 16:09

Shibili k.p