Realm Blog

Introducing Realm Swift (& Realm Objective-C 0.92)

Earlier this week, we soft-launched what should have been Realm Cocoa 0.92. Instead, this release brings bigger changes. After months of work, we’re happy to announce that Realm’s support for Swift is graduating from a simple API bridged from Objective-C, to a full-fledged, Swift-native API.

We’ve always been big fans of Swift, by supporting it from day 1, by organizing what is now the largest Swift meetup group in the world, and sharing many Swift videos, and we just felt it was time to offer a separate, Swift-first API for Realm to provide you with an easier and more intuitive experience.

To highlight this, we are introducing a separate product, with a separate download, pod & documentation, called Realm Swift. What used to be called Realm Cocoa, is now available as Realm Objective-C. Both projects will be on the same release schedule, with the same version numbers, as they are maintained as part of the same codebase at the moment.

  • Realm Swift is what you should use to build Swift apps: it has a much cleaner API, and it will be what we use to support the latest Swift Language releases. You can download Realm Swift or read the docs.
  • Realm Objective-C will continue to evolve, as quickly as Realm Cocoa did (we actually shipped a lot of new things in today’s 0.92 release). If you really need to, you will still be able to bridge into it from Swift as well, albeit with the old, not-so-Swifty API — this is useful for projects that contain a mix of Swift & Objective-C. You can download Realm Objective-C or read the docs.

A look at the brand new Realm Swift

class Employee: Object {
  dynamic var name = "" // you can specify defaults
  dynamic var startDate = NSDate()
  dynamic var salary = 0.0
  dynamic var fullTime = true
}

class Company: Object {
  dynamic var name = ""
  dynamic var ceo: Employee? // optional to-one relationship
  let employees = List<Employee>() // generic to-many relationship
}

If you’ve used Realm from Swift before, you’ll immediately recognize some improvements, including support for optionals and generic list properties.

let company = Company() // Using Realm Objects
company.name = "Realm Inc."

let realm = Realm() // Default Realm
realm.write { // Transactions
  realm.add(company)
}

// Queries
let companies = realm.objects(Company) // => Results<Company>
companies[0].name // => "Realm Inc." (property access is type-safe)
// "Jack"s who work full time (lazily loaded & chainable)
let ftJacks = realm.objects(Employee).filter("name = 'Jack'")
                .filter("fullTime = true")

Most types are named the same way (sans “RLM” prefix) in Realm Swift, but note that the class equivalent to RLMArray is called List, instead of Array.

You can read more about Realm Swift in our documentation and API reference.

Other Updates in 0.92

We’ve also been busy making many changes to Realm for Objective-C users.

API Breaking Changes

In this version, the biggest change in Objective-C is creating/updating Realm objects, which now takes the form “object with value”, rather than the previously confusing “object with object”:

  • -[RLMMigration createObject:withObject:] is now -[RLMMigration createObject:withValue:]
  • -[RLMObject initWithObject:] is now -[RLMObject initWithValue:]
  • +[RLMObject createInDefaultRealmWithObject:] is now +[RLMObject createInDefaultRealmWithValue:]
  • +[RLMObject createInRealm:withObject:] is now +[RLMObject createInRealm:withValue:]
  • +[RLMObject createOrUpdateInDefaultRealmWithObject:] is now +[RLMObject createOrUpdateInDefaultRealmWithValue:]
  • +[RLMObject createOrUpdateInRealm:withObject:] is now +[RLMObject createOrUpdateInRealm:withValue:]

We understand this might mean a number of code changes, but it’s a rename we felt was necessary before Realm hits 1.0.

In another potentially API-breaking change, migration blocks are no longer invoked when a Realm file is first created. This should only affect you if you are relying on undocumented migration behavior (for example, using a migration block to populate a Realm upon first creation).

Enhancements

  • Int8 properties defined in Swift are now treated as integers, rather than booleans.
  • NSPredicates created using +predicateWithValue: are now supported.
  • -[RLMRealm inWriteTransaction] is now public.

Bug Fixes

  • Compound AND predicates with no subpredicates now correctly match all objects.

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