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

Alex Leffelman: Migrating to Realm on iOS

Realm
April 07, 2016

Alex Leffelman: Migrating to Realm on iOS

Realm

April 07, 2016
Tweet

More Decks by Realm

Other Decks in Programming

Transcript

  1. Remind’s Starting Point - RestKit - AFNetworking 1.x - Complicated

    RestKit mapping DSL - Mogenerator-generated model classes - Modular Remind stack on top of RestKit
  2. Parallel Databases - Server is the only source of truth

    - Hook into RestKit’s exit point - Map raw JSON into Realm database
  3. Parallel Data Models: anti-DRY - User == RealmUser, Group ==

    RealmGroup, etc - Determine migration lifetime early on
  4. Integrating Initial UI Updates - Low-traffic, stable screens - Representative

    of general patterns - How to accommodate both model types? - Replication vs. Conditional Logic
  5. Interoperability @interface RLMObject (CoreDataMatching) + (NSString *)coreDataEntityName; - (NSPredicate *)coreDataMatchingPredicate;

    - (id)coreDataEntity:(NSManagedObjectContext *)context; @end @interface NSManagedObject (RealmMatching) + (NSString *)realmEntityName; - (NSPredicate *)realmMatchingPredicate; - (id)realmEntity:(RLMRealm *)realm; @end
  6. @implementation RLMObject (CoreDataMatching) - (NSPredicate *)coreDataMatchingPredicate { NSString *primaryKey =

    [[self class] primaryKey]; id primaryValue = [self valueForKeyPath:primaryKey]; return [NSPredicate predicateWithFormat:@"%K == %@", primaryKey, primaryValue]; }
  7. @implementation NSManagedObject (RealmMatching) - (__kindof RLMObject)realmEntity:(RLMRealm *)realm { NSString *entityName

    = [[self class] realmEntityName]; Class entityClass = NSClassFromString(entityName); NSPredicate *predicate = [self realmMatchingPredicate]; RLMResults *results = [entityClass objectsInRealm:realm withPredicate:predicate]; return [results firstObject]; }
  8. Finishing the Job - What To Do/Not Do - With

    high confidence, dive in - Have a plan - Don’t underestimate
  9. Non-Obvious, Non-Trivial Migration Tasks - Network replacement? - Supplementing Realm

    features - Unit Tests - Continuous integration - Code Review
  10. Cutting Out Core Data - Press forward with Realm entities?

    - Abandon Realm entities and migrate Core Data?
  11. The Wrong Process, AKA, “The Hal” - Update one class’s

    @interface to use Realm only. - Recurse every broken reference to that class - Follow the compiler until the build is green again
  12. A Better Process(?) - 2 Steps Back = 100 Steps

    Forward - Remove your Realm entities - Convert your Core Data entities directly
  13. Recap: - You can roll out a Realm migration slowly

    - Be safe out there - Don’t be an idiot like me - It’ll still take longer than you think