Alex Leffelman: Migrating to Realm on iOS

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
April 07, 2016

Alex Leffelman: Migrating to Realm on iOS

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

April 07, 2016
Tweet

Transcript

  1. Alex from Remind - alex@remindhq.com - @LeffelMania - remind.com/about

  2. Try Before You Buy How to Test-Drive Realm Alongside Core

    Data
  3. Gearing Up for a Realm Migration

  4. Why Choose?

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

    RestKit mapping DSL - Mogenerator-generated model classes - Modular Remind stack on top of RestKit
  6. Our Top Risk: Object Mapping into Realm - From-Scratch implementation

    - Inconsistent JSON data - Type coercion
  7. Server-Side Kill-Switch + Monitoring

  8. Parallel Databases - Server is the only source of truth

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

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

    of general patterns - How to accommodate both model types? - Replication vs. Conditional Logic
  11. 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
  12. @implementation RLMObject (CoreDataMatching) - (NSPredicate *)coreDataMatchingPredicate { NSString *primaryKey =

    [[self class] primaryKey]; id primaryValue = [self valueForKeyPath:primaryKey]; return [NSPredicate predicateWithFormat:@"%K == %@", primaryKey, primaryValue]; }
  13. @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]; }
  14. That’s It - Everything you need to migrate gradually.

  15. Finishing the Job - What To Do/Not Do - With

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

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

    - Abandon Realm entities and migrate Core Data?
  19. 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
  20. None
  21. None
  22. A Better Process(?) - 2 Steps Back = 100 Steps

    Forward - Remove your Realm entities - Convert your Core Data entities directly
  23. #winning

  24. 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
  25. That’s it! Thanks, Realm! - alex@remindhq.com - @LeffelMania - remind.com/careers

    - Questions?