Slide 1

Slide 1 text

iOS architecture Bryan Irace August 5, 2014

Slide 2

Slide 2 text

Purpose of this talk • The iOS codebase should not be this magical, black box that you’re scared to look at or touch • All engineers should feel comfortable reading through the code (and opening pull requests)

Slide 3

Slide 3 text

Relational DB File system Tumblr API Local storage Networking and storage Run loop (waits for user interaction)

Slide 4

Slide 4 text

Sync engine UI Sync engine User requested posts User created a post, liked/followed something, etc. New posts persisted No Internet? No problem Local storage Outgoing posts are queued, retried when connected Fetch new posts if connected, no worries if not

Slide 5

Slide 5 text

Core Data Basically an ORM, but don’t call it that

Slide 6

Slide 6 text

Our database is (mostly) just a cache Provides speed plus a nice offline experience without complication

Slide 7

Slide 7 text

Controller Controller View View MVC Controller View View View View What you see on screen and interact with Exists in a hierarchy Drawn programatically or using a visual editor (Interface Builder) Manages a screenful (or so) of information Responds to user interaction Contains only presentation logic Has a single view Populates its view with data

Slide 8

Slide 8 text

Adaptive layout • iOS 1-7 • Check if we’re running on an iPhone or iPad • Check if we’re in portrait or landscape • iOS 8 • At any point, app will be either vertically/horizontally “regular” or “compact” • This can change based on rotation, other factors

Slide 9

Slide 9 text

Post rendering

ryan:

The site may crash. 16- bitch met david.

Tumblr - making dreams come true (for David)

API response XML parser Mapping of character ranges to semantic section types (and attributes) Output string + data structure Or “Why inline embeds and stuff don’t show up”

Slide 10

Slide 10 text

ryan:

The site may crash. 16-bitch a> met da vid.

Tumblr - making dreams come true (for David)

(0, 4): [ { type: 'link', attributes: { url: ‘http://ryan.tumblr.com/post/…’ } } ], (0, 6): [ { type: 'paragraph' } ], (46, 45): [ { type: 'paragraph' } ], (6, 40): [ { type: 'blockquote' }, { type: 'paragraph' } ], (26, 8): [ { type: 'link', attributes: { url: ‘http://tmblr.co/…’ } } ], (39, 5): [ { type: 'link', attributes: { url: ‘http://tmblr.co/…’ } } ] ryan:\nThe site may crash. 16-bitch met david.\nTumblr - making dreams come true (for David)

Slide 11

Slide 11 text

TextKit NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString: @"ryan:\nThe site may crash. 16-bitch met david.\nTumblr - making dreams come true (for David)"]; ! // Loop through ranges, add the right attributes to each range ! [postContent addAttributes:@{ NSLinkAttributeName : [NSURL URLWithString:@“http://ryan.tumblr.com/post/…”] } range:NSMakeRange(0, 4)]; ! // Lots of work required to support things like nested blockquote indentation, nested lists, inline images, etc.

Slide 12

Slide 12 text

Yes, I realize we’re kind of implementing a web browser here All in the name of performance

Slide 13

Slide 13 text

GIFs • Save each individual frame as a JPEG (plus a metadata file) • Play by looping through the individual frames • Don’t read them all into memory at once

Slide 14

Slide 14 text

• Think Gems/Bundler, NPM, etc. • Used for both internal and third-party dependencies • Downloads as well as integrates with Xcode project file, compilation settings, etc.

Slide 15

Slide 15 text

Internal libraries BRYMailToURIParser*
 BRYParseKeyboardNotification*
 SnoopySDK
 TMCache*
 TMTumblrSDK*
 TMVideoPlayer
 TumblrCommon * Open source

Slide 16

Slide 16 text

Third-party libraries CHTCollectionViewWaterfallLayout
 GRMustache
 HockeySDK
 JCKeyPathValidator
 JRSwizzle
 MTMigration
 PocketAPI
 ObjectiveCHTMLParser
 Reachability

Slide 17

Slide 17 text

Application lifecycle • Apps can be purged from memory at any time, if iOS needs to reclaim memory • iOS 7 added “background fetch” – app can periodically update itself even if it had previously been killed • Apps can perform “background tasks” (within reason) • e.g. User creates a post and then backgrounds the app – post will continue uploading

Slide 18

Slide 18 text

Push notifications Notification App is foregrounded App is backgrounded App is foregrounded and given payload App is given payload immediately iOS displays notification without app’s involvement

Slide 19

Slide 19 text

Inter-app communication • Apps define protocols that they can handle, e.g. “tumblr://“ • We’ve support a number of URLs, including: • tumblr://x-callback-url/tag?tag=gif • tumblr://x-callback-url/blog?blogName=bryan • tumblr://x-callback-url/quote? quote=Quote&source=Source • x-callback-url is an unofficial specification for how iOS URLs should be structured • x-success and x-error URL parameters are used to return the user to the app where they came from

Slide 20

Slide 20 text

Inter-app communication Apps can also register to open files of a certain type, e.g. JPEG or MP4

Slide 21

Slide 21 text

Inter-app communication iOS 8 allows apps to expose “share extensions”