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

Tim Oliver: A Swift Introduction to Realm

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
September 17, 2015
13k

Tim Oliver: A Swift Introduction to Realm

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

September 17, 2015
Tweet

Transcript

  1. A Swift Introduction to Realm Tim Oliver

  2. • System Comparisons • General Introduction • Introduction to Realm

    Overview • Realm Swift Examples
  3. About Me • Joined Realm in March 2015 • From

    Perth, Australia • iOS Developer for 6 years • Loves karaoke!
  4. None
  5. iComics http://icomics.co

  6. System-level solutions

  7. System-level solutions •Serialisation (JSON/PLIST) •SQLite •Core Data

  8. 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)
  9. 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)
  10. Serialisation (JSON) •Acceptable in small cases •Not smart (All or

    nothing) •Not thread-safe •Great for minimising dependencies •Potentially slow
  11. Realm

  12. 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…)
  13. 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?
  14. 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?
  15. 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?
  16. What does it look like? Core (C++) Obj-C Swift More…

    Java
  17. Who’s using it?

  18. Who’s using it? Running on more than 500,000,000 devices!

  19. How do I use it!?

  20. Installation 1 ) Download from http://realm.io 2 ) Add framework

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

    the store on-disk. •Realm files can be opened with Realm Browser for Mac.
  22. 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
  23. 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) }
  24. 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”)
  25. 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.
  26. 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.
  27. 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) //… }
  28. 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
  29. Upcoming features •Support for NULL properties •Fine-grained notifications •Swift 2.0

    Support
  30. http://realm.io

  31. http://realm.io @realm /realm #realm

  32. Thanks for watching!

  33. Thanks for watching! to@realm.io @timoliverau Questions?