Slide 1

Slide 1 text

Consistency in an Unpredictable World

Slide 2

Slide 2 text

Hi, I’m Wendy! @wendyluwho

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Emoji & Deep Learning !

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

4 -> 3 dozen developers

Slide 10

Slide 10 text

Not all users are on the best phones, or best network Speed is important

Slide 11

Slide 11 text

"

Slide 12

Slide 12 text

Network Usage (Optimizing data sent over API) App Startup Time Concurrency

Slide 13

Slide 13 text

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)

Slide 14

Slide 14 text

Moved startup tasks to low-priority concurrent queue Reduced startup time by 50%

Slide 15

Slide 15 text

AsyncDisplayKit

Slide 16

Slide 16 text

Model Layer Immutable

Slide 17

Slide 17 text

VC 1 VC 1 Pin Pin VC 2 ?!

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Wendy Users Kanye Taylor blockUser(users[1]) #

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

A VC 2 Pin VC 1

Slide 22

Slide 22 text

Updating Models

Slide 23

Slide 23 text

Updating = Creating a new model

Slide 24

Slide 24 text

Creating from JSON Dictionary

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Creating from Builder Object

Slide 27

Slide 27 text

• 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”

Slide 28

Slide 28 text

let pin = Pin(builder:pinBuilder)

Slide 29

Slide 29 text

Loading and Caching

Slide 30

Slide 30 text

“There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors.”

Slide 31

Slide 31 text

image_url description user

Slide 32

Slide 32 text

recipe

Slide 33

Slide 33 text

PINCache

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Merge after initialization

Slide 36

Slide 36 text

Observing for Changes

Slide 37

Slide 37 text

Previously: KVO Notifications

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

notificationManager.addObserverForUpdatedModel(user, block:{ (NSNotification) in // Update profile view here! })

Slide 40

Slide 40 text

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)

Slide 41

Slide 41 text

NSNotificationCenter.defaultCenter().removeObserver(self) NSNotificationCenter.defaultCenter().removeObserver(observer)

Slide 42

Slide 42 text

notificationManager.addObserverForUpdatedModel(user, block:{ (NSNotification) in // Update profile view here! })

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

notificationManager.addObserverForUpdatedModel(user, block:{ (NSNotification) in // Update profile view here! })

Slide 45

Slide 45 text

let newUser = PIUser(builder:builder) NotificationManager.postModelUpdatedNotificationWithObject(newUser)

Slide 46

Slide 46 text

Cache postModelUpdatedNotificationWithObject(newUser) “id” = “User123” “name” = “Taylor Swift”

Slide 47

Slide 47 text

Making UI Updates

Slide 48

Slide 48 text

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) } })

Slide 49

Slide 49 text

Consistency in an Unpredictable World

Slide 50

Slide 50 text

https://engineering.pinterest.com/blog/immutable-models- and-data-consistency-our-ios-app Additional Resources https://code.facebook.com/posts/340384146140520/ making-news-feed-nearly-50-faster-on-ios/ https://github.com/linkedin/RocketData