Realm Blog

Realm Objective‑C & Swift 0.102 – Rename Properties in Migrations or Skip Them Altogether!

Support for Xcode 6.x and Swift older than 2.2 is deprecated and will be removed entirely in our next release.

We’re releasing version 0.102 of Realm Objective‑C and Realm Swift today, adding two small migration-related features: the ability to rename properties in migrations, or to configure your Realm file to be deleted whenever a migration would have previously been required. Read on for more information.

Renaming Properties in Migrations

It’s now possible to rename properties as part of migrations. Renaming a property is more efficient than copying values and preserves relationships rather than duplicating them.

To rename a property during a migration, make sure that your new models have a property by the new name and don’t have a property by the old name.

If the new property has different nullability or indexing settings, those will be applied during the rename operation.

Here’s how you could rename Person’s yearsSinceBirth property to age:

// Inside your application(application:didFinishLaunchingWithOptions:)

Realm.Configuration.defaultConfiguration = Realm.Configuration(
  schemaVersion: 1,
  migrationBlock: { migration, oldSchemaVersion in
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
      // The renaming operation should be done outside of calls to `enumerate(_:)`.
      migration.renamePropertyForClass(Person.className(), oldName: "yearsSinceBirth", newName: "age")
    }
  })
// Inside your [AppDelegate didFinishLaunchingWithOptions:]

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 1;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
  // We haven’t migrated anything yet, so oldSchemaVersion == 0
  if (oldSchemaVersion < 1) {
    // The renaming operation should be done outside of calls to `enumerateObjects:`.
    [migration renamePropertyForClass:Person.className oldName:@"yearsSinceBirth" newName:@"age"];
  }
};
[RLMRealmConfiguration setDefaultConfiguration:config];

Deleting Realms When a Migration is Required

It’s quite common to modify a Realm schema numerous times during development, but writing fully-fledged migrations at that stage is time consuming and distracting when you’re just trying to “get the job done”…

So in this version, we’re adding a deleteRealmIfMigrationNeeded boolean property to RLMRealmConfiguration/Realm.Configuration. This flag defaults to false so you won’t start suddenly losing data if you upgrade to this version of Realm, unless you explicitly opt in to this feature.

Bug Fixes

  • Fix crash when migrating to the new date format introduced in 0.101.0.
  • When using BETWEEN on an RLMArray/List property, only match when a single related object is within the request range, rather than allowing different objects in the list to satisfy the upper and lower bounds.
  • Fix crash when querying inverse relationships when objects are deleted.
  • Fix a race condition when a Realm is opened on one thread while it is in the middle of being closed on another thread.

Thanks for reading. Now go forth and build amazing apps with Realm! As always, we’re around on Stack Overflow, GitHub, or Twitter.


Realm Team

At Realm, our mission is to help developers build better apps faster. We provide a unique set of tools and platform technologies designed to make it easy for developers to build apps with sophisticated, powerful features — things like realtime collaboration, augmented reality, live data synchronization, offline experiences, messaging, and more.

Everything we build is developed with an eye toward enabling developers for what we believe the mobile internet evolves into — an open network of billions of users and trillions of devices, and realtime interactivity across them all.

Get more development news like this