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

Data Consistency in an Unpredictable World - Sw...

Wendy Lu
November 08, 2016

Data Consistency in an Unpredictable World - Swift Summit

At Pinterest, we recently completed a rewrite our iOS app (http://www.wired.com/2016/04/pinterest-reinvents-prove-really-worth-billions/). A lot of the work in an app rewrite goes unnoticed by the general public. Besides the sleek design and cool animations on the surface, one of the goals of our re-architecture was to move to a completely immutable model layer. This talk will focus on how we keep data consistent within our app with immutable models. We'll walk through updating models, loading and caching server data, and the architecture of our consistency framework.

Wendy Lu

November 08, 2016
Tweet

More Decks by Wendy Lu

Other Decks in Technology

Transcript

  1. "

  2. AVPlayer.resetAVAudioSessionCategoryToDefault() FBSDKSettings.configureForUseForApplication(application, withLaunchOptions:launchOptions) GSDAppIndexing.sharedInstance().registerApp(kAppStoreID) iRate.configureForUse() Adjust.appDidLaunch(adjustConfig) Stripe.configureForUse() DDLog.addLogger(DDTTYLogger.sharedInstance()) let fileLogger

    = DDFileLogger() fileLogger.logFileManager.maximumNumberOfLogFiles = 3 DDLog.addLogger(fileLogger) PIDeadlockDetector.enable() PICrash.sharedInstance().configureForUse() PINRemoteImageManager.configureForUse() CBLExperienceManager.configureForUse() NSValueTransformer.setValueTransformer(PIDateValueTransformer(), forName:kPINModelDateValueTransformerKey) CBLDeepLinkManager.sharedManager().configureServicesWithLaunchOp tions(launchOptions)
  3. let pin = Pin(dictionary:pinJSON) { "board" = { "created_at" =

    "Tue, 13 Aug 2013 16:38:36 +0000"; "id" = 418131215342691718; "name" = "spaces"; }; "comment_count" = 0; "description" = "At the top of my wish list for this fall is a giant chunky knit wool blanket."; "id" = "AVpd31ttshLHlWbcG9g_Kt3uVzZHjfHNvzwT20p6YnO6qzvQnqs_Z5A"; "image_square_url" = "https://s-media-cache-ak0.pinimg.com/b58cc94084407a39d62c83885ce4699e.jpg"; } Pin JSON
  4. • imageURL = “https://www.123.com” Pin (Immutable) PinBuilder (Mutable) • title

    = “The best pin in the world” • board = “Cute Cats” • imageURL = “https://www.123.com” • title = • board = “Cute Cats” “The best pin in the world” “Meow”
  5. “There are only two hard things in computer science: cache

    invalidation, naming things, and off-by-one errors.”
  6. Cache "id": "123" Pin123 "id" = "123" "image_url" : "http://new-url.com"

    "recipe" : {"ingredients": ["bananas"]} "imageURL" = "http://old-url.com" "board" = "Cakery" "id" = "123" "image_url" : "http://new-url.com" “board" = "Cakery" "recipe" : {"ingredients": ["bananas"]} Pin JSON
  7. NSNotificationCenter.defaultCenter().addObserverForName("name", object: nil, queue: nil) { note in // ...

    } public func addObserverForName(name: String?, object obj: AnyObject?, queue: NSOperationQueue?, usingBlock block: (NSNotification) -> Void) -> NSObjectProtocol (__NSObserver)
  8. class NotificationManager: NSObject { private var observerTokens: [String: AnyObject] =

    [:] deinit { unregisterAll() } func unregisterAll() { for token in observerTokens.values { NSNotificationCenter.defaultCenter().removeObserver(token) } } } http://moreindirection.blogspot.com/2014/08/nsnotificationcenter-swift-and-blocks.html
  9. notificationManager.addObserverForUpdatedModel(user, block: { [weak self] notification in if let user

    = notification.object as? PIUser { self?.user = user // Update profile view here! self?.titleLabel.text = user.name self?.imageView.setImageWithURL(user.imageURL) } })