Slide 1

Slide 1 text

What I Learned From AFNetworking's GitHub Issues Mattt Thompson Mobile Lead, Heroku

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Lessons Learned

Slide 6

Slide 6 text

Lesson 1: Designing Great Software Means Saying No... A Lot

Slide 7

Slide 7 text

Every Line of Code Needs to Justify its Own Existence

Slide 8

Slide 8 text

Every Feature Adds Complexity

Slide 9

Slide 9 text

• Ordered Query String Parameters • Customizable Query String Format • Property for DELETE Parameters to be in either Query String or Message Body • Retry Parameter for Operations • Option to Parse Top-Level JSON Fragments

Slide 10

Slide 10 text

ARC w/Macros

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Lesson 2: If You Can’t Give Them What They Want, Give Them Something Better

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Feature Requests are Data Points to Understand What Should Be Improved

Slide 15

Slide 15 text

Custom Mime Types

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

• application/json • application/javascript

Slide 18

Slide 18 text

• application/json • application/javascript • text/javascript

Slide 19

Slide 19 text

• application/json • application/javascript • text/javascript • text/x-javascript • text/x-json • application/x-javascript

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

application/vnd.github+json

Slide 22

Slide 22 text

AFHTTPRequestOperation +addAcceptableContentTypes:

Slide 23

Slide 23 text

JSON Libraries

Slide 24

Slide 24 text

• JSONKit • yajl_json • SBJSON • TouchJSON • NextiveJSON

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

AFJSONUtilities.h AFJSONEncode() / AFJSONDecode()

Slide 27

Slide 27 text

NSData * AFJSONEncode(id object, NSError **error) { NSData *data = nil; SEL _JSONKitSelector = NSSelectorFromString(@"JSONDataWithOptions:error:"); SEL _YAJLSelector = NSSelectorFromString(@"yajl_JSONString"); id _SBJsonWriterClass = NSClassFromString(@"SBJsonWriter"); SEL _SBJsonWriterSelector = NSSelectorFromString(@"dataWithObject:"); id _NXJsonSerializerClass = NSClassFromString(@"NXJsonSerializer"); SEL _NXJsonSerializerSelector = NSSelectorFromString(@"serialize:"); id _NSJSONSerializationClass = NSClassFromString(@"NSJSONSerialization"); SEL _NSJSONSerializationSelector = NSSelectorFromString(@"dataWithJSONObject:options:error:"); #ifdef _AFNETWORKING_PREFER_NSJSONSERIALIZATION_ if (_NSJSONSerializationClass && [_NSJSONSerializationClass respondsToSelector:_NSJSONSerializationSelector]) { goto _af_nsjson_encode; } #endif if (_JSONKitSelector && [object respondsToSelector:_JSONKitSelector]) { NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[object methodSignatureForSelector:_JSONKitSelector]]; invocation.target = object; invocation.selector = _JSONKitSelector; NSUInteger serializeOptionFlags = 0; [invocation setArgument:&serializeOptionFlags atIndex:2]; // arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation if (error != NULL) { [invocation setArgument:error atIndex:3]; } [invocation invoke]; [invocation getReturnValue:&data]; } else if (_SBJsonWriterClass && [_SBJsonWriterClass instancesRespondToSelector:_SBJsonWriterSelector]) { id writer = [[_SBJsonWriterClass alloc] init]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[writer methodSignatureForSelector:_SBJsonWriterSelector]]; invocation.target = writer; invocation.selector = _SBJsonWriterSelector; [invocation setArgument:&object atIndex:2]; // arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation [invocation invoke]; [invocation getReturnValue:&data]; [writer release];

Slide 28

Slide 28 text

NSJSONSerialization

Slide 29

Slide 29 text

ASINetworkQueue

Slide 30

Slide 30 text

ASIHTTPRequest

Slide 31

Slide 31 text

ASINetworkQueue

Slide 32

Slide 32 text

AFHTTPClient -enqueueBatchOfHTTPRequestOperations: progressBlock: completionBlock:

Slide 33

Slide 33 text

Lesson 3: Never Merge In Code That You Don’t Understand

Slide 34

Slide 34 text

Lesson 3: Never Merge In Code That You Don’t Understand

Slide 35

Slide 35 text

dispatch_group

Slide 36

Slide 36 text

AFHTTPClient -enqueueBatchOfHTTPRequestOperations: progressBlock: completionBlock:

Slide 37

Slide 37 text

1 2 3 4 5 Batch

Slide 38

Slide 38 text

1 2 3 4 5 Batch

Slide 39

Slide 39 text

1 2 3 4 5 Batch 1 2 3 4 5 1 2 3 4 5

Slide 40

Slide 40 text

Thread Safety

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Lesson 4: Don’t Settle For Not Understanding Something

Slide 44

Slide 44 text

Thread Safety

Slide 45

Slide 45 text

MVC Networking

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

• dispatch_async pattern • Dedicated Network Thread • Custom Dispatch Queues for Background Processing

Slide 52

Slide 52 text

“The Deallocation Problem”

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

When you start a secondary thread, it's common for that thread to retain the target object. If you don’t ensure that the thread releases that reference before the main thread releases its last reference to the object, bad things happen.

Slide 55

Slide 55 text

- (void)setCompletionBlock:(void (^)(void))block { if (!block) { [super setCompletionBlock:nil]; } __block id _blockSelf = self; [super setCompletionBlock:^ { block(); [_blockSelf setCompletionBlock:nil]; }]; }

Slide 56

Slide 56 text

API Consistency

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

AFHTTPClient -HTTPRequestOperationWithRequest: success: failure: AFHTTPRequestOperation -setCompletionBlockWithSuccess: failure:

Slide 59

Slide 59 text

Lesson 5: The More You Put In, The More You Get Out

Slide 60

Slide 60 text

•Be Present •Be Nice •Be Socratic 3 Rules for Project Ownership

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Thanks!