Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Realm in Swift

Realm in Swift

Budapest Talk 11/21/2014

Avatar for Joseph Anderson

Joseph Anderson

November 29, 2014
Tweet

More Decks by Joseph Anderson

Other Decks in Programming

Transcript

  1. Realm in Swift Build better apps faster Joe Anderson iOS

    @ Realm Swift Meetup Budapest 11/21/2014 Flickr image: Moyan Brenn https://flic.kr/p/djcHTx
  2. Core Data Full featured & mature 10+ years old ORM

    for SQLite Slow Complex and difficult to learn/debug Apple only
  3. SQLite Faster than Core Data Optimized for iOS Cross Platform

    14+ years old Bad User Experience Manual mapping and queries Lack of thread safety
  4. What is Realm exactly? Fast, zero-copy, embedded database NoSQL Full

    ACID Transactions Simple Multithreading Model Universal Data - Cross Platform w/ C++ Core For Objective-C, Swift, Android
  5. Model Simplicity class Person : RLMObject { dynamic var name

    = “” dynamic var saying = “” dynamic var type = “” dynamic var views = 0 }
  6. Model Simplicity class Dog : RLMObject { dynamic var name

    = “” dynamic var age: int = 0 dynamic var owner: Person? } class Person : RLMObject { dynamic var name = “” dynamic var saying = “” dynamic var dogs = RLMArray(objectClassName: Dog.className()) }
  7. Model Simplicity let realm = Realm.defaultRealm() let celebrity = Person()

    celebrity.name = “Sweet Brown” celebrity.saying = “Ain’t nobody got time for that” realm.beginWriteTransaction() realm.addObject(celebrity) realm.commitWriteTransaction()
  8. Queries with Predicates var internetCelebs = Person.objectsWhere(“type = 'internet' AND

    name BEGINSWITH ’S’") let tonsOfViewsPred: NSPredicate = NSPredicate(format: “views > %@'', 10000000) var superStars = internetCelebs.objectsWithPredicate(tonsOfViewsPred)
  9. How its possible Most are building on top of SQLite

    zero copy architecture bitpacking reduces memory usage up to 90% caching and vectorization makes it faster than native C universal data representation from multiple languages Photo by Natalie Lucier https://flic.kr/p/6vUu56
  10. Common use cases Offline mode Caching (JSON/Object Mapping) Upload later

    Predictive Data Games Wearables Bluetooth Low Energy
  11. JSON Mapper { name: “Sweet Brown”, saying: “Ain’t nobody got

    time for that”, dogs: [ { name: “Petey”, age: 1 }, { name: “Lucy”, age: 5 } ] …
  12. JSON Mapper Works with any object not just JSON. Key-Value

    realm.beginWriteTransaction() Person.createInDefaultRealmWithObject(personFromApi) realm.commitWriteTransaction()
  13. Breezeworks “Run your entire service business from your phone” Uses

    Realm to embed & query large databases of tax rates & service codes 2m raised so far breezeworks.com j.mp/realm-breezeworks
  14. Beanflow Full POS & inventory app (w/ offline mode) Uses

    Realm as network cache for all data “I don’t have any plans to go back to Core Data anytime soon. I really like Realm” sebastiandobrincu.com www.beanflow.com
  15. Cloth Personal outfit diary Popular app with large user base

    Uses Realm for all data handling took over 2 year-old codebase and ported it to Realm in 1 day clothapp.com reallyseth.com
  16. Biggest mistakes people make when implementing Realm Creating foreign keys

    rather than thinking object based Ask whats the faulting model? People aren’t used to thinking of their objects in memory when it comes to databases
  17. Migrations //v1 class Person : RLMObject { dynamic firstName =

    “” dynamic lastName = “” dynamic age: int = 0 } //v2 class Person : RLMObject { dynamic fullName = “” dynamic age: int = 0 }
  18. Minimum Migration RLMRealm.setSchemaVersion(1, withMigrationBlock: { migration, oldSchemaVersion in // We

    haven’t migrated anything yet, so oldSchemaVersion == 0 if (oldSchemaVersion < 1) { // Nothing to do! // Realm will automatically detect new properties and removed properties // And will update the schema on disk automatically } })
  19. Migrating Data Realm.setSchemaVersion(1, withMigrationBlock: { migration, oldSchemaVersion in if oldSchemaVersion

    < 1 { // The enumerateObjects:block: method iterates // over every 'Person' object stored in the Realm file migration.enumerateObjects(Person.className()) { oldObject, newObject in // combine name fields into a single field let firstName = oldObject[@"firstName"] as String let lastName = oldObject[@"lastName"] as String newObject[@"fullName"] = “\(firstName) \(lastName)” } } })