283
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 Session 227 Architecting for Performance on watchOS 3 Tyler McAtee watchOS Engineer Todd Grooms watchOS Engineer

Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

App Frameworks #WWDC16

Session 227

Architecting for Performance on watchOS 3

Tyler McAtee watchOS EngineerTodd Grooms watchOS Engineer

Page 2: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Agenda

Page 3: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Agenda

2-Second Tasks

Page 4: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Agenda

2-Second TasksDesign

Page 5: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Agenda

2-Second TasksDesignCase Study

Page 6: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

Page 7: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

What are they?2-Second Tasks

Page 8: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

What are they?2-Second Tasks

Purposeful

Page 9: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

What are they?2-Second Tasks

PurposefulQuick, short, simple

Page 10: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

What are they?2-Second Tasks

PurposefulQuick, short, simpleMeasured from beginning to end

Page 11: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Examples2-Second Tasks

Page 12: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Examples2-Second Tasks

Check a notification

Page 13: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Examples2-Second Tasks

Check a notificationSet a timer

Page 14: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Examples2-Second Tasks

Check a notificationSet a timerStart a workout

Page 15: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second TasksImprovements

Page 16: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

Complications for all

Improvements

Page 17: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

Complications for allDock

Improvements

Page 18: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

Complications for allDock

Improvements

Page 19: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

Page 20: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

ComplicationApp

ComplicationApp

Dock App

Dock App

Dock App

Dock App

Page 21: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

ComplicationApp

ComplicationApp Dock App

Dock App

Dock AppDock App

Page 22: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

ComplicationApp

ComplicationApp Dock App

Dock App

Dock AppDock App

Dock App

Dock App

Dock App

Dock App

Dock App Dock App

Page 23: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

ComplicationApp

ComplicationApp Dock App

Dock App

Dock AppDock App

Dock App

Dock App

Dock App

Dock App

Dock App Dock App

ComplicationApp

ComplicationApp

ComplicationApp

Page 24: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

ComplicationApp

ComplicationApp Dock App

Dock App

Dock AppDock App

Dock App

Dock App

Dock App

Dock App

Dock App Dock App

ComplicationApp

ComplicationApp

ComplicationApp

System App

ProcessProcess

Mail/Calendar

Sync

Workout Session

Process Process

Process

Background Task

Process

ProcessProcess

Process

Process

Process

Page 25: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 26: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

LimitMemory

Page 27: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

LimitMemory

WatchKit applications have a fixed memory limit

Page 28: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

LimitMemory

WatchKit applications have a fixed memory limitShould be nowhere near the limit

Page 29: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

LimitMemory

WatchKit applications have a fixed memory limitShould be nowhere near the limitCurrent limit is 30MB

Page 30: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StrategiesMemory

Page 31: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StrategiesMemory

Use appropriately sized images

Page 32: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StrategiesMemory

Use appropriately sized imagesUse appropriately sized data sets

Page 33: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StrategiesMemory

Use appropriately sized imagesUse appropriately sized data setsUse lightweight APIs

Page 34: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StrategiesMemory

Use appropriately sized imagesUse appropriately sized data setsUse lightweight APIsDon't keep around things you no longer need

Page 35: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 36: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

ComplicationApp

ComplicationApp

Dock App

Dock App

Dock App

Dock App

Page 37: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Key Path in watchOS 3Resume Time

ComplicationApp

ComplicationApp

Dock App

Dock App

Dock App

Dock App

Page 38: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Key Path in watchOS 3Resume Time

Page 39: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resuming often in the DockResume Time

Page 40: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resuming often in the DockResume Time

Page 41: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle extension delegate methodsResume Time

Page 42: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle extension delegate methodsResume Time

applicationDidFinishLaunching

Page 43: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle extension delegate methodsResume Time

applicationDidFinishLaunchingapplicationDidBecomeActive

Page 44: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle extension delegate methodsResume Time

applicationDidFinishLaunchingapplicationDidBecomeActiveapplicationWillResignActive

Page 45: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle extension delegate methodsResume Time

applicationDidFinishLaunchingapplicationDidBecomeActiveapplicationWillResignActiveapplicationDidEnterBackground

Page 46: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle extension delegate methodsResume Time

applicationDidFinishLaunchingapplicationDidBecomeActiveapplicationWillResignActiveapplicationDidEnterBackgroundapplicationWillEnterForeground

Page 47: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

Page 48: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:

Page 49: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivate

Page 50: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppear

Page 51: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppear

Page 52: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppear

Page 53: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppearwillDisappear

Page 54: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppearwillDisappeardidDeactivate

Page 55: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppearwillDisappeardidDeactivate

Page 56: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Lifecycle interface controller methodsResume Time

awakeWithContext:willActivatedidAppearwillDisappeardidDeactivate

Page 57: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 58: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 59: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 60: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

applicationDidFinishLaunching

applicationDidBecomeActive

Page 61: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

awakeWithContext:

willActivate

didAppear

Page 62: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 63: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 64: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 65: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

applicationWillResignActive

Page 66: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 67: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 68: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willDisappear

didDeactivate

Page 69: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

applicationDidEnterBackground

Page 70: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 71: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Keeping Your Watch App Up to Date Mission Thursday 9:00AM

Snapshot!

Page 72: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Keeping Your Watch App Up to Date Mission Thursday 9:00AM

Snapshot!

willActivate

didAppear

Page 73: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Keeping Your Watch App Up to Date Mission Thursday 9:00AM

Snapshot!

handleBackgroundTasks:

Page 74: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Keeping Your Watch App Up to Date Mission Thursday 9:00AM

Snapshot!

willDisappear

didDeactivate

Page 75: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 76: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

applicationWillEnterForeground

Page 77: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willActivate

didAppear

Page 78: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 79: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

applicationDidBecomeActive

Page 80: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 81: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Already Set!!

Page 82: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Use discretion when updating WKInterface objects

Already Set!!

Page 83: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Use discretion when updating WKInterface objects

Already Set!!

WatchKitExtension

WatchKit UI

Page 84: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Use discretion when updating WKInterface objects

Already Set!!

WatchKitExtension

WatchKit UI

Page 85: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Use discretion when updating WKInterface objects

Already Set!!

Page 86: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Page 87: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

WKInterfaceTable is not a UITableView

Page 88: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

WKInterfaceTable is not a UITableView

Page 89: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

WKInterfaceTable is not a UITableView

Page 90: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

WKInterfaceTable is not a UITableView

Page 91: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

WKInterfaceTable is not a UITableView

Page 92: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Keep WKInterfaceTable size down

WKInterfaceTable is not a UITableView

Page 93: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Keep WKInterfaceTable size down

Avoid reloading a WKInterfaceTable when possible

WKInterfaceTable is not a UITableView

Page 94: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Keep WKInterfaceTable size down

Avoid reloading a WKInterfaceTable when possible

WKInterfaceTable is not a UITableView

Page 95: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

TipsResume Time

Keep WKInterfaceTable size down

Avoid reloading a WKInterfaceTable when possible

WKInterfaceTable is not a UITableView

Page 96: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Page 97: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Page 98: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UI

Page 99: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UIFocused purpose

Page 100: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UIFocused purposeNavigation

Page 101: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UIFocused purposeNavigation

Page 102: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UIFocused purposeNavigation

Page 103: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UIFocused purposeNavigation

Page 104: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Design

Glanceable UIFocused purposeNavigation

Quick Interaction Techniques for watchOS Presidio Wednesday 11:00AM

Page 105: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 106: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 107: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 108: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

contextForSegue:withIdentifier:inTable:

Page 109: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

contextForSegue:withIdentifier:inTable:

Page 110: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 111: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

awakeWithContext

Page 112: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willActivate

Page 113: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didAppear

Page 114: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

awakeWithContext

Page 115: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

awakeWithContext

Page 116: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didDeactivatewillActivate

Page 117: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didDeactivate

Page 118: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willActivate

Page 119: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didDeactivate

Page 120: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 121: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 122: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willDisappear

Page 123: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willActivate

Page 124: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didDeactivate

Page 125: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didAppear

Page 126: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 127: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

willDisappear

Page 128: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

didDeactivate

Page 129: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second
Page 130: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

A Watch App built with WatchKitCase Study: Stocks

Page 131: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Case study outlineStocks

Page 132: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Case study outlineStocks

2-second tasks

Page 133: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Case study outlineStocks

2-second tasksBackground refresh

Page 134: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Case study outlineStocks

2-second tasksBackground refreshResume time optimizations

Page 135: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-Second Tasks

Page 136: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-second tasksStocks

Complication Chart/Detail List View

Page 137: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Complication2-Second Tasks

Fastest way to check your favorite stock’s current priceData is in sync between complication and app

Page 138: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Complication2-Second Tasks

Fastest way to check your favorite stock’s current priceData is in sync between complication and app

Keeping Your Watch App Up to Date Mission Thursday 9:00AM

Page 139: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Navigation flow2-Second Tasks

watchOS 2

Page 140: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Navigation flow2-Second Tasks

watchOS 2

Page 141: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Navigation flow2-Second Tasks

watchOS 2

Page 142: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Navigation flow2-Second Tasks

watchOS 2

Page 143: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

watchOS 2

Navigation flow2-Second Tasks

Page 144: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

watchOS 2

Navigation flow2-Second Tasks

watchOS 3

Page 145: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

watchOS 2

Navigation flow2-Second Tasks

watchOS 3

Page 146: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

In the Dock2-Second Tasks

Page 147: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

In the Dock2-Second Tasks

Page 148: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-second tasks recapStocks

Page 149: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-second tasks recapStocks

Consistent data between complication and app

Page 150: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-second tasks recapStocks

Consistent data between complication and appSimplified our design

Page 151: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

2-second tasks recapStocks

Consistent data between complication and appSimplified our designImplemented new Vertical Detail Paging API

Page 152: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Background Refresh

Page 153: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Background refreshStocks

Page 154: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Background refreshStocks

How often do we need to update?

Page 155: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Background refreshStocks

How often do we need to update?What data do we need to fetch to keep our app fresh?

Page 156: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Refresh cadenceBackground Refresh

12:00 AM 12:00 AM12:00 PM

Page 157: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Refresh cadenceBackground Refresh

Request data every 15 minutes

12:00 AM 12:00 AM12:00 PM

Page 158: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Refresh cadenceBackground Refresh

Request data every 15 minutesMarkets are open for a period of time throughout the day

12:00 AM 12:00 AM12:00 PM

Page 159: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Refresh cadenceBackground Refresh

Request data every 15 minutesMarkets are open for a period of time throughout the day

12:00 AM 12:00 AM12:00 PM

Page 160: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Refresh cadenceBackground Refresh

Request data every 15 minutesMarkets are open for a period of time throughout the dayThere are days when the market is not open

12:00 AM 12:00 AM12:00 PM

Page 161: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Decide next refresh dateBackground Refresh

Page 162: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Decide next refresh dateBackground Refresh

Enumerate through list of stocks

Page 163: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Decide next refresh dateBackground Refresh

Enumerate through list of stocksIf markets are all closed, use earliest market open time

Page 164: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Decide next refresh dateBackground Refresh

Enumerate through list of stocksIf markets are all closed, use earliest market open timeElse at least one market is open, use regular 15 minute cadence

Page 165: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule background refresh

func scheduleBackgroundRefresh(preferredDate: NSDate?) {

if let preferredDate = preferredDate {

let completion: (NSError?) -> Void = { error in

// Handle error if needed

}

WKExtension.shared().scheduleBackgroundRefresh(

withPreferredDate: preferredDate,

userInfo: nil,

scheduledCompletion: completion

)

}

}}

Page 166: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule background refresh

func scheduleBackgroundRefresh(preferredDate: NSDate?) {

if let preferredDate = preferredDate {

let completion: (NSError?) -> Void = { error in

// Handle error if needed

}

WKExtension.shared().scheduleBackgroundRefresh(

withPreferredDate: preferredDate,

userInfo: nil,

scheduledCompletion: completion

)

}

}}

Page 167: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule background refresh

func scheduleBackgroundRefresh(preferredDate: NSDate?) {

if let preferredDate = preferredDate {

let completion: (NSError?) -> Void = { error in

// Handle error if needed

}

WKExtension.shared().scheduleBackgroundRefresh(

withPreferredDate: preferredDate,

userInfo: nil,

scheduledCompletion: completion

)

}

}}

Page 168: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule background refresh

func scheduleBackgroundRefresh(preferredDate: NSDate?) {

if let preferredDate = preferredDate {

let completion: (NSError?) -> Void = { error in

// Handle error if needed

}

WKExtension.shared().scheduleBackgroundRefresh(

withPreferredDate: preferredDate,

userInfo: nil,

scheduledCompletion: completion

)

}

}}

Page 169: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Helper method to grab preferred refresh date

func nextPreferredRefreshDate() -> NSDate? {

guard let earliestNextOpenDateInStocks = self.earliestNextOpenDateInStocks() else {

return nil

}

let nextRegularRefreshDate = NSDate(timeIntervalSinceNow: RefreshTimeInterval)

return earliestNextOpenDateInStocks.laterDate(nextRegularRefreshDate)

}

Page 170: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Helper method to grab preferred refresh date

func nextPreferredRefreshDate() -> NSDate? {

guard let earliestNextOpenDateInStocks = self.earliestNextOpenDateInStocks() else {

return nil

}

let nextRegularRefreshDate = NSDate(timeIntervalSinceNow: RefreshTimeInterval)

return earliestNextOpenDateInStocks.laterDate(nextRegularRefreshDate)

}

Page 171: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Helper method to grab preferred refresh date

func nextPreferredRefreshDate() -> NSDate? {

guard let earliestNextOpenDateInStocks = self.earliestNextOpenDateInStocks() else {

return nil

}

let nextRegularRefreshDate = NSDate(timeIntervalSinceNow: RefreshTimeInterval)

return earliestNextOpenDateInStocks.laterDate(nextRegularRefreshDate)

}

Page 172: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Helper method to grab preferred refresh date

func nextPreferredRefreshDate() -> NSDate? {

guard let earliestNextOpenDateInStocks = self.earliestNextOpenDateInStocks() else {

return nil

}

let nextRegularRefreshDate = NSDate(timeIntervalSinceNow: RefreshTimeInterval)

return earliestNextOpenDateInStocks.laterDate(nextRegularRefreshDate)

}

Page 173: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Helper method to grab preferred refresh date

func nextPreferredRefreshDate() -> NSDate? {

guard let earliestNextOpenDateInStocks = self.earliestNextOpenDateInStocks() else {

return nil

}

let nextRegularRefreshDate = NSDate(timeIntervalSinceNow: RefreshTimeInterval)

return earliestNextOpenDateInStocks.laterDate(nextRegularRefreshDate)

}

Page 174: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 175: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 176: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 177: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 178: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 179: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 180: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Calculate the “next open date” for a user’s list of stocks

func earliestNextOpenDateInStocks() -> NSDate? {

let stocks = self.stocksManager.stocks

guard stocks.count > 0 else {

return nil

}

var earliestNextOpenDate = NSDate.distantFuture()

for stock in stocks {

guard !stock.marketIsOpen else {

// If market is open, return distantPast

return NSDate.distantPast()

}

if let nextMarketOpenDate = stock.nextMarketOpenDate {

earliestNextOpenDate = nextMarketOpenDate.earlierDate(earliestNextOpenDate)

}

}

return earliestNextOpenDate

}

Page 181: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Schedule multiple background requestsBackground Refresh

Page 182: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Schedule multiple background requestsBackground Refresh

Endpoint A updates the app Endpoint B updates the complication

Page 183: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Schedule multiple background requestsBackground Refresh

Schedule background refresh timeOn receipt• Submit Endpoint A request• Submit Endpoint B request• Schedule future background refresh time

Page 184: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 185: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 186: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 187: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 188: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 189: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 190: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 191: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 192: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// WKExtensionDelegate Handle Background Tasks

func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

for task in backgroundTasks {

switch task {

case let appRefreshTask as WKApplicationRefreshBackgroundTask:

self.scheduleDataUpdateRequest()

self.scheduleBackgroundRefresh(preferredDate: self.nextPreferredRefreshDate())

appRefreshTask.setTaskCompleted()

case let urlSessionTask as WKURLSessionRefreshBackgroundTask:

self.storeURLSessionTask(urlSessionTask: urlSessionTask)

default:

task.setTaskCompleted()

}

}

}

Page 193: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Schedule app update background requestBackground Refresh

Schedule requestsOn complete of requests• Complete WKURLSessionRefreshBackgroundTask• Schedule snapshot• Reload complication

Page 194: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule the network request to keep the app snapshot up-to-date

func scheduleDataUpdateRequest() {

// Setup download tasks

self.setupAppDataRequest()

self.setupComplicationDataRequest()

// Setup finishUpdateHandler

self.finishUpdateHandler = { sessionIdentifier -> Void in

if let taskToComplete = self.urlSessionTasks[sessionIdentifier] {

self.scheduleSnapshot()

self.reloadComplication()

taskToComplete.setTaskCompleted()

}

}

self.submitRequests()

}

Page 195: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule the network request to keep the app snapshot up-to-date

func scheduleDataUpdateRequest() {

// Setup download tasks

self.setupAppDataRequest()

self.setupComplicationDataRequest()

// Setup finishUpdateHandler

self.finishUpdateHandler = { sessionIdentifier -> Void in

if let taskToComplete = self.urlSessionTasks[sessionIdentifier] {

self.scheduleSnapshot()

self.reloadComplication()

taskToComplete.setTaskCompleted()

}

}

self.submitRequests()

}

Page 196: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule the network request to keep the app snapshot up-to-date

func scheduleDataUpdateRequest() {

// Setup download tasks

self.setupAppDataRequest()

self.setupComplicationDataRequest()

// Setup finishUpdateHandler

self.finishUpdateHandler = { sessionIdentifier -> Void in

if let taskToComplete = self.urlSessionTasks[sessionIdentifier] {

self.scheduleSnapshot()

self.reloadComplication()

taskToComplete.setTaskCompleted()

}

}

self.submitRequests()

}

Page 197: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule the network request to keep the app snapshot up-to-date

func scheduleDataUpdateRequest() {

// Setup download tasks

self.setupAppDataRequest()

self.setupComplicationDataRequest()

// Setup finishUpdateHandler

self.finishUpdateHandler = { sessionIdentifier -> Void in

if let taskToComplete = self.urlSessionTasks[sessionIdentifier] {

self.scheduleSnapshot()

self.reloadComplication()

taskToComplete.setTaskCompleted()

}

}

self.submitRequests()

}

Page 198: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule the network request to keep the app snapshot up-to-date

func scheduleDataUpdateRequest() {

// Setup download tasks

self.setupAppDataRequest()

self.setupComplicationDataRequest()

// Setup finishUpdateHandler

self.finishUpdateHandler = { sessionIdentifier -> Void in

if let taskToComplete = self.urlSessionTasks[sessionIdentifier] {

self.scheduleSnapshot()

self.reloadComplication()

taskToComplete.setTaskCompleted()

}

}

self.submitRequests()

}

Page 199: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Schedule the network request to keep the app snapshot up-to-date

func scheduleDataUpdateRequest() {

// Setup download tasks

self.setupAppDataRequest()

self.setupComplicationDataRequest()

// Setup finishUpdateHandler

self.finishUpdateHandler = { sessionIdentifier -> Void in

if let taskToComplete = self.urlSessionTasks[sessionIdentifier] {

self.scheduleSnapshot()

self.reloadComplication()

taskToComplete.setTaskCompleted()

}

}

self.submitRequests()

}

Page 200: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// NSURLSessionDelegate Background Download Tasks Complete

@objc func urlSessionDidFinishEvents(forBackgroundURLSession session: NSURLSession) {

if let identifier = session.configuration.identifier,

finishUpdateHandler = self.finishUpdateHandler {

finishUpdateHandler(identifier)

}

}

Page 201: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// NSURLSessionDelegate Background Download Tasks Complete

@objc func urlSessionDidFinishEvents(forBackgroundURLSession session: NSURLSession) {

if let identifier = session.configuration.identifier,

finishUpdateHandler = self.finishUpdateHandler {

finishUpdateHandler(identifier)

}

}

Page 202: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// NSURLSessionDelegate Background Download Tasks Complete

@objc func urlSessionDidFinishEvents(forBackgroundURLSession session: NSURLSession) {

if let identifier = session.configuration.identifier,

finishUpdateHandler = self.finishUpdateHandler {

finishUpdateHandler(identifier)

}

}

Page 203: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Background refresh recapStocks

Optimize how often you schedule updates for your appIf updating with data from a server, try to use single specialized endpoint

Page 204: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume Time Optimizations

Page 205: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume timeStocks

Minimize work during willActivate and didAppear• Avoid long running tasks that are triggered from willActivate• Smart load/reload of data• Only set properties on WKInterfaceObjects that have changed

Page 206: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Cautionary tale for Vertical Detail Paging APIResume Time

Neighboring detail pages will have willActivate calledAvoid expensive operations in willActivate for detail pages

Page 207: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Cautionary tale for Vertical Detail Paging APIResume Time

Neighboring detail pages will have willActivate calledAvoid expensive operations in willActivate for detail pages

Page 208: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Cautionary tale for Vertical Detail Paging APIResume Time

Reports of slow loading chart when entering first detail pageOther detail pages never finished loading their charts

Page 209: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

downloadAndGenerateChart()

}

override func didAppear() {

super.didAppear()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

Page 210: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

downloadAndGenerateChart()

}

override func didAppear() {

super.didAppear()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

Page 211: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

downloadAndGenerateChart()

}

override func didAppear() {

super.didAppear()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

Page 212: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

}

override func didAppear() {

super.didAppear()

downloadAndGenerateChart()

}

override func willDisappear() {

super.willDisappear()

cancelDownloadAndGenerateChart()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

func cancelDownloadAndGenerateChart() {

//Cancel long running task to download chart data and generate chart image

}

Page 213: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

}

override func didAppear() {

super.didAppear()

downloadAndGenerateChart()

}

override func willDisappear() {

super.willDisappear()

cancelDownloadAndGenerateChart()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

func cancelDownloadAndGenerateChart() {

//Cancel long running task to download chart data and generate chart image

}

Page 214: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

}

override func didAppear() {

super.didAppear()

downloadAndGenerateChart()

}

override func willDisappear() {

super.willDisappear()

cancelDownloadAndGenerateChart()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

func cancelDownloadAndGenerateChart() {

//Cancel long running task to download chart data and generate chart image

}

Page 215: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - willActivate/didAppear in StockInterfaceController.swift

override func willActivate() {

super.willActivate()

}

override func didAppear() {

super.didAppear()

downloadAndGenerateChart()

}

override func willDisappear() {

super.willDisappear()

cancelDownloadAndGenerateChart()

}

func downloadAndGenerateChart() {

// Long running task to download chart data and generate chart image

}

func cancelDownloadAndGenerateChart() {

//Cancel long running task to download chart data and generate chart image

}

Page 216: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Vertical Detail Paging API caveatsResume Time

Page 217: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Vertical Detail Paging API caveatsResume Time

Avoid triggering long running tasks in willActivate

Page 218: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Vertical Detail Paging API caveatsResume Time

Avoid triggering long running tasks in willActivateMake use of cancelable operations

Page 219: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

WKInterfaceTable loadingResume Time

Page 220: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

WKInterfaceTable loadingResume Time

All rows are loaded in memory

Page 221: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

WKInterfaceTable loadingResume Time

All rows are loaded in memoryThere is a linear upfront cost to the number of rows you have in your table

Page 222: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

WKInterfaceTable loadingResume Time

All rows are loaded in memoryThere is a linear upfront cost to the number of rows you have in your tableNo reuse

Page 223: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

WKInterfaceTable loadingResume Time

Initial Launch Time of Stocks

Tim

e

5.5s

6s

6.5s

7s

Number of Stocks in List0 1 5 10

Page 224: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Page 225: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Limit the number of rows you load

Page 226: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Limit the number of rows you loadDo smart updates of your table when row deltas occur

Page 227: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - Load Stocks Table

func loadTable() {

let stocks = self.stocksManager.stocks

self.table.setNumberOfRows(stocks.count, withRowType: stockRowControllerIdentifier)

for (index, stock) in stocks.enumerated() {

self.populateStockRowController(index: index, stock: stock)

}

}

Page 228: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - Load Stocks Table

func loadTable() {

let stocks = self.stocksManager.stocks

self.table.setNumberOfRows(stocks.count, withRowType: stockRowControllerIdentifier)

for (index, stock) in stocks.enumerated() {

self.populateStockRowController(index: index, stock: stock)

}

}

Page 229: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - Load Stocks Table

func loadTable() {

let stocks = self.stocksManager.stocks

self.table.setNumberOfRows(stocks.count, withRowType: stockRowControllerIdentifier)

for (index, stock) in stocks.enumerated() {

self.populateStockRowController(index: index, stock: stock)

}

}

Page 230: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Page 231: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Number of stocks in list is not capped

Page 232: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Number of stocks in list is not cappedAlways usingsetNumberOfRows(numberOfRows: Int, withRowType rowType: String)

Page 233: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table

func loadTableSmart() {

let stocks = self.stocksManager.stocks

let stocksCount = min(stocks.count, maxStocksListSize)

let stockRowDelta = stocksCount - self.table.numberOfRows

self.insertRemoveTableRows(stockRowDelta: stockRowDelta)

for ( index, stock ) in stocks.enumerated() {

guard index < maxStocksListSize else {

break

}

self.populateStockRowController(index: index, stock: stock)

}

}

Page 234: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table

func loadTableSmart() {

let stocks = self.stocksManager.stocks

let stocksCount = min(stocks.count, maxStocksListSize)

let stockRowDelta = stocksCount - self.table.numberOfRows

self.insertRemoveTableRows(stockRowDelta: stockRowDelta)

for ( index, stock ) in stocks.enumerated() {

guard index < maxStocksListSize else {

break

}

self.populateStockRowController(index: index, stock: stock)

}

}

Page 235: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table

func loadTableSmart() {

let stocks = self.stocksManager.stocks

let stocksCount = min(stocks.count, maxStocksListSize)

let stockRowDelta = stocksCount - self.table.numberOfRows

self.insertRemoveTableRows(stockRowDelta: stockRowDelta)

for ( index, stock ) in stocks.enumerated() {

guard index < maxStocksListSize else {

break

}

self.populateStockRowController(index: index, stock: stock)

}

}

Page 236: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table

func loadTableSmart() {

let stocks = self.stocksManager.stocks

let stocksCount = min(stocks.count, maxStocksListSize)

let stockRowDelta = stocksCount - self.table.numberOfRows

self.insertRemoveTableRows(stockRowDelta: stockRowDelta)

for ( index, stock ) in stocks.enumerated() {

guard index < maxStocksListSize else {

break

}

self.populateStockRowController(index: index, stock: stock)

}

}

Page 237: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table

func loadTableSmart() {

let stocks = self.stocksManager.stocks

let stocksCount = min(stocks.count, maxStocksListSize)

let stockRowDelta = stocksCount - self.table.numberOfRows

self.insertRemoveTableRows(stockRowDelta: stockRowDelta)

for ( index, stock ) in stocks.enumerated() {

guard index < maxStocksListSize else {

break

}

self.populateStockRowController(index: index, stock: stock)

}

}

Page 238: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table

func loadTableSmart() {

let stocks = self.stocksManager.stocks

let stocksCount = min(stocks.count, maxStocksListSize)

let stockRowDelta = stocksCount - self.table.numberOfRows

self.insertRemoveTableRows(stockRowDelta: stockRowDelta)

for ( index, stock ) in stocks.enumerated() {

guard index < maxStocksListSize else {

break

}

self.populateStockRowController(index: index, stock: stock)

}

}

Page 239: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table - insert/remove table rows

func insertRemoveTableRows(stockRowDelta: Int) {

let stockRowChangeRange = NSRange(location: 0, length: abs(stockRowDelta))

let stockRowChangeIndexSet = NSIndexSet(indexesIn: stockRowChangeRange)

if stockRowDelta > 0 {

self.table.insertRows(

at: stockRowChangeIndexSet,

withRowType: stockRowControllerIdentifier

)

}

else if stockRowDelta < 0 {

self.table.removeRows(at: stockRowChangeIndexSet)

}

}

Page 240: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table - insert/remove table rows

func insertRemoveTableRows(stockRowDelta: Int) {

let stockRowChangeRange = NSRange(location: 0, length: abs(stockRowDelta))

let stockRowChangeIndexSet = NSIndexSet(indexesIn: stockRowChangeRange)

if stockRowDelta > 0 {

self.table.insertRows(

at: stockRowChangeIndexSet,

withRowType: stockRowControllerIdentifier

)

}

else if stockRowDelta < 0 {

self.table.removeRows(at: stockRowChangeIndexSet)

}

}

Page 241: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table - insert/remove table rows

func insertRemoveTableRows(stockRowDelta: Int) {

let stockRowChangeRange = NSRange(location: 0, length: abs(stockRowDelta))

let stockRowChangeIndexSet = NSIndexSet(indexesIn: stockRowChangeRange)

if stockRowDelta > 0 {

self.table.insertRows(

at: stockRowChangeIndexSet,

withRowType: stockRowControllerIdentifier

)

}

else if stockRowDelta < 0 {

self.table.removeRows(at: stockRowChangeIndexSet)

}

}

Page 242: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table - insert/remove table rows

func insertRemoveTableRows(stockRowDelta: Int) {

let stockRowChangeRange = NSRange(location: 0, length: abs(stockRowDelta))

let stockRowChangeIndexSet = NSIndexSet(indexesIn: stockRowChangeRange)

if stockRowDelta > 0 {

self.table.insertRows(

at: stockRowChangeIndexSet,

withRowType: stockRowControllerIdentifier

)

}

else if stockRowDelta < 0 {

self.table.removeRows(at: stockRowChangeIndexSet)

}

}

Page 243: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table - insert/remove table rows

func insertRemoveTableRows(stockRowDelta: Int) {

let stockRowChangeRange = NSRange(location: 0, length: abs(stockRowDelta))

let stockRowChangeIndexSet = NSIndexSet(indexesIn: stockRowChangeRange)

if stockRowDelta > 0 {

self.table.insertRows(

at: stockRowChangeIndexSet,

withRowType: stockRowControllerIdentifier

)

}

else if stockRowDelta < 0 {

self.table.removeRows(at: stockRowChangeIndexSet)

}

}

Page 244: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Second Approach - Load Stocks Table - insert/remove table rows

func insertRemoveTableRows(stockRowDelta: Int) {

let stockRowChangeRange = NSRange(location: 0, length: abs(stockRowDelta))

let stockRowChangeIndexSet = NSIndexSet(indexesIn: stockRowChangeRange)

if stockRowDelta > 0 {

self.table.insertRows(

at: stockRowChangeIndexSet,

withRowType: stockRowControllerIdentifier

)

}

else if stockRowDelta < 0 {

self.table.removeRows(at: stockRowChangeIndexSet)

}

}

Page 245: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Page 246: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Number of stocks in list is capped

Page 247: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Number of stocks in list is cappedInserting/removing rows is much more efficient than reloading the entire table

Page 248: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Page 249: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Instead of iterating over entire table when single rows are updated, consider:

Page 250: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Instead of iterating over entire table when single rows are updated, consider:• Use rowController(at index: Int) -> AnyObject? to get RowController

to be updated

Page 251: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve WKInterfaceTable loading performanceResume Time

Instead of iterating over entire table when single rows are updated, consider:• Use rowController(at index: Int) -> AnyObject? to get RowController

to be updated• Store a reference to the RowController to update later

Page 252: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Updating your UI elementsResume Time

WKInterfaceObjects are modified in the extension processUpdates to these properties are sent from extension process to app processApp process handles layout of interface

Already Set!!

Page 253: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Updating your UI elementsResume Time

WKInterfaceObjects are modified in the extension processUpdates to these properties are sent from extension process to app processApp process handles layout of interface

Already Set!!

WatchKitExtension

WatchKit UI

Page 254: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 255: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 256: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 257: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 258: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 259: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 260: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 261: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

StocksInterfaceController layoutResume Time

@IBOutlet weak var platter: WKInterfaceGroup!

@IBOutlet weak var listNameLabel: WKInterfaceLabel!

@IBOutlet weak var changeInPointsLabel: WKInterfaceLabel!

@IBOutlet weak var priceLabel: WKInterfaceLabel!

Page 262: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Initial Approach - Update StockRowController

func update(listName: String, price: String, changeInPoints: String,

changeLabelColor: UIColor, platterColor: UIColor) {

self.platter.setBackgroundColor(platterColor)

self.listNameLabel.setText(listName)

self.changeInPointsLabel.setText(changeInPoints)

self.changeInPointsLabel.setTextColor(changeLabelColor)

self.priceLabel.setText(price)

}

Page 263: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve layout update performanceResume Time

Page 264: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve layout update performanceResume Time

Properties on WKInterfaceObject are not cached

Page 265: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve layout update performanceResume Time

Properties on WKInterfaceObject are not cachedSetting a property on WKInterfaceObject sends that value to the app process every time

Page 266: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve layout update performanceResume Time

Properties on WKInterfaceObject are not cachedSetting a property on WKInterfaceObject sends that value to the app process every timeOn average, 200ms for value to move from extension to app process

Page 267: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Improve layout update performanceResume Time

Properties on WKInterfaceObject are not cachedSetting a property on WKInterfaceObject sends that value to the app process every timeOn average, 200ms for value to move from extension to app process1.4s worst case scenario when initially loading the stocks list

Page 268: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - Update StockRowController Part 1

func update(listName: String, price: String, changeInPoints: String, changeLabelColor:

UIColor, platterColor: UIColor) {

if self.platterColor?.hash != platterColor.hash {

self.platterColor = platterColor

self.platter.setBackgroundColor(platterColor)

}

if self.listName?.hash != listName.hash {

self.listName = listName

self.listNameLabel.setText(listName)

}

// ...

}

Page 269: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - Update StockRowController Part 1

func update(listName: String, price: String, changeInPoints: String, changeLabelColor:

UIColor, platterColor: UIColor) {

if self.platterColor?.hash != platterColor.hash {

self.platterColor = platterColor

self.platter.setBackgroundColor(platterColor)

}

if self.listName?.hash != listName.hash {

self.listName = listName

self.listNameLabel.setText(listName)

}

// ...

}

Page 270: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - Update StockRowController Part 1

func update(listName: String, price: String, changeInPoints: String, changeLabelColor:

UIColor, platterColor: UIColor) {

if self.platterColor?.hash != platterColor.hash {

self.platterColor = platterColor

self.platter.setBackgroundColor(platterColor)

}

if self.listName?.hash != listName.hash {

self.listName = listName

self.listNameLabel.setText(listName)

}

// ...

}

Page 271: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

// Better Approach - Update StockRowController Part 1

func update(listName: String, price: String, changeInPoints: String, changeLabelColor:

UIColor, platterColor: UIColor) {

if self.platterColor?.hash != platterColor.hash {

self.platterColor = platterColor

self.platter.setBackgroundColor(platterColor)

}

if self.listName?.hash != listName.hash {

self.listName = listName

self.listNameLabel.setText(listName)

}

// ...

}

Page 272: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume time recapStocks

Page 273: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume time recapStocks

Minimize work performed willActivate and didAppear

Page 274: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume time recapStocks

Minimize work performed willActivate and didAppearMake use of cancelable operations

Page 275: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume time recapStocks

Minimize work performed willActivate and didAppearMake use of cancelable operationsOverly complicated user interfaces will lead to slower resume times

Page 276: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Resume time recapStocks

Minimize work performed willActivate and didAppearMake use of cancelable operationsOverly complicated user interfaces will lead to slower resume timesOnly update your user interface when necessary

Page 277: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Summary

Page 278: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Summary

Think small• Keep tasks small and easy to perform• Simplify your user interface• Make use of new Background Refresh APIs

Page 279: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Summary

Think small• Keep tasks small and easy to perform• Simplify your user interface• Make use of new Background Refresh APIs

Focus on resume time• Pay attention to WKInterfaceController lifecycle methods (especially willActivate and didAppear)

• Make use of cancelable operations• Optimize when updating your user interface

Page 280: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

More Information

https://developer.apple.com/wwdc16/227

Page 281: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Related Sessions

What’s New in watchOS 3 Presidio Tuesday 5:00PM

Quick Interaction Techniques for watchOS Presidio Wednesday 11:00AM

Designing Great Apple Watch Experiences Presidio Wednesday 1:40PM

Keeping Your Watch App Up to Date Mission Thursday 9:00AM

Concurrent Programming With GCD in Swift 3 Pacific Heights Friday 4:00PM

Advanced NSOperations WWDC 2015

Page 282: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second

Labs

WatchKit and WatchConnectivity Lab Frameworks Lab B Friday 2:00PM

Page 283: Architecting for Performance on watchOS 3€¦ · Redistribution or public display not permitted without written permission from Apple. App Frameworks #WWDC16 ... Agenda 2-Second