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

Realm.io - The brand new database for mobile apps

Realm.io - The brand new database for mobile apps

Slides of my talk @CocoaheadsParis on Thursday, November 13th talking about the awesome Realm framework !

Clément Sauvage

November 13, 2014
Tweet

More Decks by Clément Sauvage

Other Decks in Programming

Transcript

  1. Realm.io The brand new database for mobile apps © Clément

    Sauvage - Cocoaheads Paris - Nov. 2014
  2. This conf could be named : Data persistency #FingerInTheNose ©

    Clément Sauvage - Cocoaheads Paris - Nov. 2014
  3. Who's that guy ? Clément Sauvage 24 years old Lille,

    France Web developer since 2004 iOS & OS X Developer since 2009 @clementsauvage @csauvage on GitHub © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  4. A bit more Founded an mobile agency Kalokod Mostly iOS/Android

    & Web consulting Teacher @ Supinfo (Where I studied); Teacher @ #SupDeWeb Cocoaheads Lille Organizer © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  5. SQLite storage engine But no official wrapper (Ok there's FMBD)

    © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  6. CoreData Not properly a storage engine Quite difficult to learn

    © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  7. Amazingly simple My little sister could do it (Uh... wait...

    I haven't any sister) © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  8. Data model You know how to design ObjC classes right

    ? So, you know how to use Realm ! @interface CSPeople:RLMObject @property NSString *name; @property NSString *job; @property int age; @property float height @property (ignored) float AMEXCardNumber That's all #FingerInTheNose © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  9. Writing Let's record that in a realm CSPeople *obiWanKenobi =

    [[CSPeople alloc]init]; obiWanKenobi.name = @"Obi Wan Kenobi"; obiWanKenobi.job = @"Jedi"; obiWanKenobi.age = 57; obiWanKenobi.height = 1.82; RLMRealm *starWarsVII = [RLMRealm defaultRealm]; //Once per thread [starWarsVII beginWriteTransaction]; [starWarsVII addObject:author]; [starWarsVII commitWriteTransaction]; © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  10. Queries As simple as an NSPredicate RLMArray *starWarsQuery = [starWarsVII

    objectsWhere:@"job = 'jedi' AND name BEGINSWITH 'O'"]; And using an NSPredicate object NSPredicate *jediPredicate = [NSPredicate predicateWithFormat:@"job = %@ AND name BEGINSWITH %@", @"jedi", @"O"] RLMArray *starWarsQuery2 = [starWarsVII objectsWithPredicate:jediPredicate]; © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  11. Sorted Request Querying is good, but what about sorting ?

    There's an app method for that ! RLMArray *sortedSiths = [[starWarsVII objectsWhere:@"job = 'sith'"] arraySortedByProperty:@"name" ascending:YES]; © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  12. Nested Calls RLMArray *SWVIISith = [starWarsVII objectsWhere:@"job = 'sith'"]; RLMArray

    *SWVIIBSith = [SWVIISith objectsWhere:@"name BEGINSWITH 'B'"]; Awesome isn't it ? © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  13. Many to one Let me introduce the Death Star, a

    spaceship ! @interface CSSpaceShip:RLMObject @property NSString *name; @property CSPeople *pilot; @property int numberOfPassengers; @property float speed; © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  14. Many to many Ok, let's imagine we want to store

    all pasengers of a spaceShip Back to our CSPeople model @interface CSPeople:RLMObject @property NSString *name; (...) @property (ignored) float AMEXCardNumber @end RLM_ARRAY_TYPE(CSPeople) © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  15. Many to many Back to our CSSpaceShip model @interface CSSpaceShip:RLMObject

    @property NSString *name; @property CSPeople *pilot; @property int numberOfPassengers; @property float speed; @property RLMArray<CSPeople> *passengers © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  16. Many to many Let's add few stormtroopers RLMArray *stormTroopers =

    [StarWarsVII objectsWhere:@"jobs = 'stormstrooper'"]; [deathStar.passengers addObjectsFromArray:stormTroopers]; #FingerInTheNose © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  17. // In [AppDelegate didFinishLaunchingWithOptions:] [RLMRealm migrateDefaultRealmWithBlock:^NSUInteger(RLMMigration *migration, NSUInteger oldSchemaVersion) {

    // We haven’t migrated anything yet, so oldSchemaVersion == 0 if (oldSchemaVersion < 1) { /* The enumerateObjects:block: method iterates over every 'CSPerson' object stored in the Realm file */ [migration enumerateObjects:CSPerson.className block:^(RLMObject *oldObject, RLMObject *newObject) { // combine name fields into a single field newObject[@"fullName"] = [NSString stringWithFormat:@"%@ (%@)", oldObject[@"name"], oldObject[@"job"]]; }]; } // Return the latest version number (always set manually) // Must be a higher than the previous version or an RLMException is thrown return 1; }]; © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  18. There's a lot of things to say about Realm (Browser,

    Notifications, Background Operations, Usage w/ RESTful APIs...) But not enough time, unfortunately ! © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  19. Hey ! I already use Core Data/SQLite, why the hell

    should I change ? © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  20. One more thing... I have few T-Shirts & Stickers for

    you ! © Clément Sauvage - Cocoaheads Paris - Nov. 2014
  21. Thanks for listening Follow @clementsauvage on Twitter Find my thoughts

    on http://csauvage.github.io © Clément Sauvage - Cocoaheads Paris - Nov. 2014