Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Realm Comprehensive Intro

Realm Comprehensive Intro

An introduction to Realm in Objective-C

Avatar for Joseph Anderson

Joseph Anderson

October 27, 2014
Tweet

More Decks by Joseph Anderson

Other Decks in Programming

Transcript

  1. Realm A modern database that keeps things simple Joe Anderson

    iOS @ Realm SD iOS SIG San Diego 10/27/2014
  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 Multithreading Friendly Universal Data - Cross Platform w/ C++ Core For Objective-C, Swift, Android
  5. Model Simplicity @interface Dog : RLMObject @property NSString *name @property

    int *age @end RLM_ARRAY_TYPE(Dog) @interface Person : RLMObject @property NSString *name @property NSString *saying @property RLMArray<Dog> *dogs @end
  6. Model Simplicity RLMRealm *realm = [RLMRealm defaultRealm] [realm beginWriteTransaction] Person

    *celebrity = [Person new] celebrity.name = @“Sweet Brown” celebrity.saying = @“Ain’t nobody got time for that” [realm addObject:celebrity] [realm commitWriteTransaction]
  7. Queries with Predicates RLMResults *internetCelebs = [Person objectsWhere:@''type = 'internet'

    AND name BEGINSWITH 'S''']; NSPredicate *tonsOfViewsPred = [NSPredicate predicateWithFormat:@''views > %@'', @''10000000'']; RLMResults *superStars = [internetCelebs objectsWithPredicate:tonsOfViewsPred];
  8. 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
  9. Common use cases Offline mode Caching (JSON/Object Mapping) Upload later

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

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

    [realm beginWriteTransaction]; [Person createInDefaultRealmWithObject:personFromApi]; [realm commitWriteTransaction];
  12. 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
  13. 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
  14. 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
  15. 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
  16. Migrations //v1 @interface Person : RLMObject @property NSString *firstName; @property

    NSString *lastName; @property int age; @end //v2 @interface Person : RLMObject @property NSString *fullName; @property int age; @end
  17. Minimum Migration [RLMRealm setSchemaVersion:1 withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) { //

    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 } }];
  18. Migrating Data [RLMRealm setSchemaVersion:1 withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) { if

    (oldSchemaVersion < 1) { // The enumerateObjects:block: method iterates // over every 'Person' object stored in the Realm file [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) { // combine name fields into a single field newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]]; }]; } }];
  19. Work in progress Change notifications/Live results sets Delete Rules Bi-directional

    relationships Open Source Core Enhanced Data Browser Sync
  20. Questions answered Fetching in Core Data vs Realm? Core Data,

    writes the query, builds object graph, overhead there. For us, running the query builds up an array of row indexes. None of the data is loaded until you access it. Whats the difference between Realm & faulting exactly? In Core Data after faulting, when you access the object it fully loads and caches the object. With Realm we lazy load each value as you read it. How do we handle binary data, like optimization with images? We don’t do anything with that, we also don’t recommend storing large amounts of binary data in a realm What happens when you run out of storage while doing a migration? Full ACID guarantees, so the migration will fail and data is safe in its existing state, not overwritten