Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducing Realm: A Fast, Modern Database for Apps

Tim Oliver
September 01, 2015

Introducing Realm: A Fast, Modern Database for Apps

In September 2015, I presented at /dev/world/ about the iOS/OS X version of Realm, and provided a brief introduction into its API.

Tim Oliver

September 01, 2015
Tweet

More Decks by Tim Oliver

Other Decks in Technology

Transcript

  1. /dev/world Introducing Realm: A Fast, Modern Database for Apps Tim

    Oliver Realm Aug 31-Sep 1, 2015 @TimOliverAU
  2. Serialisation (JSON) •Acceptable in small cases •Not smart (All or

    nothing) •Not thread-safe •Great for minimising dependencies •Potentially slow
  3. SQLite •Moderately difficult (C, query language, structure) •Schema migrations need

    to be managed •Many third party libraries •Query results need to be mapped to models •Queries are very fast (If set up properly)
  4. Core Data •Massive learning curve •Operation is very opaque •Passing

    data between threads is ‘tricky’ •Lots of boilerplate code •Doesn’t fail outright (But doesn’t recover)
  5. Realm •An open source (-ish) database framework •A complete replacement

    for Core Data/SQLite •Implemented from scratch, based on ORM model •Fast performance, simple API, thread-safe •FREE!!!!11!! •Cross-platform (Mac, iOS, Android, more soon…)
  6. Tests run on an iPad Air with iOS 7.1, using

    the latest available version of each library as of July 11, 2014 How fast?
  7. Tests run on an iPad Air with iOS 7.1, using

    the latest available version of each library as of July 11, 2014 How fast?
  8. Tests run on an iPad Air with iOS 7.1, using

    the latest available version of each library as of July 11, 2014 How fast?
  9. Installation 1 ) Download from http://realm.io 2 ) Add framework

    to project (And libc++) Also available via CocoaPods and Carthage
  10. The basics - Realm objects let realm = Realm() •Represents

    the store on-disk. •Realm files can be opened with Realm Browser for Mac.
  11. The basics - Model objects class Dog: Object { dynamic

    var name = “” dynamic var age = 0 dynamic var birthdate = NSDate.date() dynamic var height = 0.0 dynamic var vaccinated = true } Subclass ‘Object’ and add your properties. Object properties are now KVO-compliant!
  12. Adding new objects to a Realm //Create new Dog object

    let newDog = Dog() newDog.name = "Earl Yippington III" //Write to Realm file let realm = Realm() realm.write { realm.add(newDog) }
  13. Reading objects from a Realm //Get all Dog objects saved

    to disk let dogs = Realm().objects(Dog) //Filter the dogs by age let puppies = dogs.filter(“age < 2”) //Sort the puppies by name let sortedPuppies = puppies.sort(“name”)
  14. Updating objects in a Realm //Get the first dog in

    the database let dog = Realm().objects(Dog).first //INCORRECT - An exception will be thrown dog.name = “Jabba the Mutt” //Correct - Object will be updated Realm().write { dog.name = “Jabba the Mutt” } Modifying Realm model objects must be done inside a write transaction.
  15. Passing objects across threads •All Realm objects are thread-confined. •An

    exception will be called if trying to access an object across threads. •Best practice is to re-fetch the same Realm object on the new thread.
  16. Passing objects across threads //Define a dog object with a

    primary key class Dog: Object { dynamic var uuid = NSUUID().UUIDString override static func primaryKey() { return “uuid” } } //Pass a Dog object between threads let dogUUID = myDog.uuid 
 dispatch_async(dispatch_get_global_queue(0,0)) { let myNewDog = Realm().objectForPrimaryKey(Dog, dogUUID) //… }
  17. Schema Migrations •All Realm files have a schema version. •An

    exception will be thrown if a Realm model object and a Realm object mismatch is detected. •A Realm configuration object allows the supplying of a block that will be called to migrate existing data to a new schema. •Sample code available at http://realm.io/docs/swift