Share
Embed
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
Transcript
38 slides
-
Realm.io The brand new database for mobile apps © Clément
Sauvage - Cocoaheads Paris - Nov. 2014 -
This conf could be named : Data persistency #FingerInTheNose ©
Clément Sauvage - Cocoaheads Paris - Nov. 2014 -
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 -
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 -
Let's talk Database & data persistency © Clément Sauvage -
Cocoaheads Paris - Nov. 2014 -
iOS Data Storage Few ways © Clément Sauvage - Cocoaheads
Paris - Nov. 2014 -
.plist files (Yep, it still exist !!) © Clément Sauvage
- Cocoaheads Paris - Nov. 2014 -
SQLite storage engine But no official wrapper (Ok there's FMBD)
© Clément Sauvage - Cocoaheads Paris - Nov. 2014 -
CoreData Not properly a storage engine Quite difficult to learn
© Clément Sauvage - Cocoaheads Paris - Nov. 2014 -
And now Let me introduce... © Clément Sauvage - Cocoaheads
Paris - Nov. 2014 -
Amazingly simple My little sister could do it (Uh... wait...
I haven't any sister) © Clément Sauvage - Cocoaheads Paris - Nov. 2014 -
Instanciate it [Realm defaultRealm] or [Realm realmWithPath:@"cocoaheadsMembers.realm"] © Clément Sauvage
- Cocoaheads Paris - Nov. 2014 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
// 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 -
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 -
Realm is available now, for free © Clément Sauvage -
Cocoaheads Paris - Nov. 2014 -
Hey ! I already use Core Data/SQLite, why the hell
should I change ? © Clément Sauvage - Cocoaheads Paris - Nov. 2014 -
One more thing... I have few T-Shirts & Stickers for
you ! © Clément Sauvage - Cocoaheads Paris - Nov. 2014 -
Thanks for listening Follow @clementsauvage on Twitter Find my thoughts
on http://csauvage.github.io © Clément Sauvage - Cocoaheads Paris - Nov. 2014