Slide 1

Slide 1 text

iOS backend wrappers for hipsters* Denis Lebedev,! iOS Dev @ Wargaming

Slide 2

Slide 2 text

Agenda • What we do! • Code & libs! • Infrastructure & tools! • Distribution

Slide 3

Slide 3 text

Service Mobile Apps department • “Assistant” apps for player engagement! • One of the largest WG API consumers! • Quality is one of the main goals! • New tools & approaches are welcome

Slide 4

Slide 4 text

3rd party APIs

Slide 5

Slide 5 text

WGNAPIClient • Separate project! • Developed with love & unit tests in mind! • Will be open sourced some day

Slide 6

Slide 6 text

Requirements • Different cache invalidation time for entities! • Wraps every available method of API! • As stable as possible (API “unexpectedly changes”)! • Supports “partial entities”! • Out-of-the-box usage

Slide 7

Slide 7 text

WGNAPIClient 0.5 • Based on AFNetworking! • Only WOT API! • Core Data as cache system (tried even SQL)! • “Manual” entities parsing! • Extremely low code reuse! • Unit tests code is awful

Slide 8

Slide 8 text

Architecture WGNClient WGNPlayersOperation WGNDataParser WGNPlayer WGNCache The same for each entity

Slide 9

Slide 9 text

Async CoreData cache • NSManagedObjectIDs here and there! • Tries to fetch existing item with ID and updates it if it exists! • Object-graph is only partially filled

Slide 10

Slide 10 text

Old-school parsing WOWPPlane *plane = [[WOWPPlane alloc] init]; ! plane.name = json[@“name"]; ! if ([json[@"type"] isEqualToString:@"assault"]) { plane.type = WOWPPlaneTypeAssault; } else { plane.type = WOWPPlaneTypeFighter; } ! plane.priceCredit = json[@"price"];

Slide 11

Slide 11 text

Block-based APIs • Cooler than delegates!! • Good for callbacks! • Everyone likes blocks

Slide 12

Slide 12 text

[WOWPClient tanksWithCompletion:^(NSArray * tanks) { }];

Slide 13

Slide 13 text

[WOWPClient tanksWithCompletion:^(NSArray * tanks) { } error:^(NSError *error){ }];

Slide 14

Slide 14 text

[WOWPClient usersWithCompletion:^(NSArray *users){ [WOWPClient clansWithCompletion:^(NSArray * clans) { [WOWPClient tanksWithCompletion:^(NSArray * tanks) { } error:^{}]; } error:^{}]; } error:^{}];

Slide 15

Slide 15 text

BOOM

Slide 16

Slide 16 text

WGNAPIClient 1.0 • Nice, clean architecture! • Well-tested! • Includes Core/Tanks/Planes components

Slide 17

Slide 17 text

Architecture WGNClient WOWPlayer WOWPClient WOTClient WOTPlayer

Slide 18

Slide 18 text

Reactive Cocoa https://github.com/ReactiveCocoa/ReactiveCocoa

Slide 19

Slide 19 text

What? • Composes and transforms streams of values! • Functional Reactive Programming in Obj-C http://en.wikipedia.org/wiki/ Functional_reactive_programming

Slide 20

Slide 20 text

Signals • Represent data that will be delivered later! • Can be chained/merged! • Have optional next/error/completed “callbacks”

Slide 21

Slide 21 text

[RACObserve(self, newName) subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];

Slide 22

Slide 22 text

[[RACObserve(self, language) ! filter:^(NSString *newName) { return [newName hasPrefix:@"j"]; }] ! subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];

Slide 23

Slide 23 text

- (RACSignal *)fetchPlaneDetails:(NSArray *)IDs; - (RACSignal *)fetchPlanes; ! … WGNAIClient public API

Slide 24

Slide 24 text

Not so nested callbacks ! [[[[WOTClient sharedClient] fetchPlayers:nil] ! flattenMap:^RACStream *(NSArray *players) { return [[WOTClient sharedClient] fetchClans:nil]; ! }] flattenMap:^RACStream *(id clans) { return [[WOTClient sharedClient] fetchClans:nil]; }];

Slide 25

Slide 25 text

One point of error/result handling ! [[[[[WOTClient sharedClient] fetchPlayers:nil] ! flattenMap:^RACStream *(NSArray *players) { return [[WOTClient sharedClient] fetchClans:nil]; ! }] flattenMap:^RACStream *(id clans) { return [[WOTClient sharedClient] fetchClans:nil]; ! }] subscribeNext:^(id tanks) { //success } error:^(NSError *error) { //error }];

Slide 26

Slide 26 text

Under the hood - (RACSignal *)fetchPlanesWithFields:(NSArray *)fields { ! return [self getPath:@“encyclopedia/planes" ! parameters:@{@"fields": PARAM(fields)} ! resultClass:WOWPShortPlane.class]; }

Slide 27

Slide 27 text

https://gist.github.com/garnett/7265957 Bind RAC to AFNetworking

Slide 28

Slide 28 text

Caching 2.0 • Say “NO” to CoreData! • Cache all the things! • Uniform approach for any entity

Slide 29

Slide 29 text

Mantle! https://github.com/github/Mantle

Slide 30

Slide 30 text

Mantle benefits • Maps backend entities to model layer! • Nested models! • Attribute transformers! • NSManagedObject support! • Implements

Slide 31

Slide 31 text

How to use 1. Create MTLModel subclass! 2. Implement protocol! 3. Use MTLJSONAdapter to parse

Slide 32

Slide 32 text

MTLModel *parsedObject = [MTLJSONAdapter modelOfClass:resultClass fromJSONDictionary:JSONDictionary error:&error]; + (NSDictionary *)JSONKeyPathsByPropertyKey { return [ @{ @"clanID": @"clan_id", @"name" : @"nickname"}]; }

Slide 33

Slide 33 text

“Mantle doesn't automatically persist your objects for you.”

Slide 34

Slide 34 text

NSURLCache • Works on the network protocol level! • Caches response by it’s URL! • 2 LOC to implement* NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:kInMemoryCacheSize diskCapacity:kOnDiskCacheSize diskPath:path]; [NSURLCache setSharedURLCache:urlCache];

Slide 35

Slide 35 text

But … • Depends on backend implementation! • Response parsed again after each request

Slide 36

Slide 36 text

Tweak NSURLCache • Add necessary cache headers on client side! • Full control of caching time! • Reference implementation: https:// gist.github.com/garnett/7266332

Slide 37

Slide 37 text

Unit tests • Test models! • Test “public” signals! • One unit-test for the cache

Slide 38

Slide 38 text

Async tests of block-based APIs! =! PAIN

Slide 39

Slide 39 text

Async tests of block-based APIs! was! PAIN

Slide 40

Slide 40 text

Unit-test lifesavers • Kiwi (shouldEventually matcher)! • OHTTPStubs ! • ReactiveCocoa

Slide 41

Slide 41 text

stubResponse(@"account/list", @“players.json"); ! RACSignal *searchRequest = [client searchPlayers:@"garnett" limit:0]; ! NSArray *response = [searchRequest asynchronousFirstOrDefault:nil success:&success error:&error]; ! [error shouldBeNil]; [[response should] beKindOfClass:NSArray.class]; [[response should] haveCountOf:3];

Slide 42

Slide 42 text

OHTTPStubs shortcuts https://gist.github.com/garnett/7267579

Slide 43

Slide 43 text

Continuous integration • Teamcity! • xctool! • gcov (code coverage)

Slide 44

Slide 44 text

xctool • Same interface as in xcodebuild! • No mess with simulator! • Fancy (or even custom) build output

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

gcov • Helps to track what’s not tested! • Managers like “green lights”

Slide 47

Slide 47 text

http://habrahabr.ru/post/150438/

Slide 48

Slide 48 text

Distribution • CocoaPods + subspecs pod ‘WGNAPIClient/WOT’ pod ‘WGNAPIClient/WOWP’

Slide 49

Slide 49 text

Hipster picture

Slide 50

Slide 50 text

Thanks!