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

A Swift Introduction to Realm

Tim Oliver
September 18, 2015

A Swift Introduction to Realm

Delivered at the Copenhagen Cocoaheads meetup in September 2015, this presentation introduced the core concepts of Realm and its Cocoa SDK.

Tim Oliver

September 18, 2015
Tweet

More Decks by Tim Oliver

Other Decks in Technology

Transcript

  1. About Me • Joined Realm in March 2015 • From

    Perth, Australia • iOS Developer for 6 years • Loves karaoke!
  2. 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)
  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. Serialisation (JSON) •Acceptable in small cases •Not smart (All or

    nothing) •Not thread-safe •Great for minimising dependencies •Potentially slow
  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! •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 let puppies = List<Dog>() } Subclass ‘Object’ and add your properties. Int8 Int16 Int32 Int64 List<Object> Object Bool Double Float String NSDate NSData Supported Types
  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