This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significant performance improvments and API redesign
Removing Q*Operation dependency
- Loading branch information
Showing
30 changed files
with
10,314 additions
and
7,695 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// AFHTTPOperation.h | ||
// | ||
// Copyright (c) 2011 Gowalla (http://gowalla.com/) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
extern NSString * const AFHTTPOperationDidStartNotification; | ||
extern NSString * const AFHTTPOperationDidFinishNotification; | ||
|
||
@interface AFHTTPRequestOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate> { | ||
@private | ||
NSURLConnection *_connection; | ||
NSPort *_port; | ||
NSSet *_runLoopModes; | ||
|
||
NSURLRequest *_request; | ||
NSHTTPURLResponse *_response; | ||
|
||
NSData *_responseBody; | ||
NSMutableData *_dataAccumulator; | ||
} | ||
|
||
@property (nonatomic, retain) NSURLConnection *connection; | ||
@property (nonatomic, retain) NSSet *runLoopModes; | ||
|
||
@property (nonatomic, retain) NSURLRequest *request; | ||
@property (nonatomic, retain) NSHTTPURLResponse *response; | ||
@property (nonatomic, retain) NSError *error; | ||
|
||
@property (nonatomic, retain) NSData *responseBody; | ||
@property (readonly) NSString *responseString; | ||
|
||
+ (id)operationWithRequest:(NSURLRequest *)urlRequest | ||
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion; | ||
|
||
- (id)initWithRequest:(NSURLRequest *)urlRequest; | ||
|
||
@end |
Oops, something went wrong.
3f901c8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, awesome to see AFNetworking getting so much more advanced. I'd love to hear more about the decision to move away from QHTTPOperation just out of academic interest. Any comments on how the new system improves performance?
3f901c8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to do a formal write-up, since writing this has been an amazing learning experience on the ins-and-outs of runloops, blocks, NSOperations, and NSURLConnection's delegate functions.
The short version is that QHTTPRequest seemed to do a lot of extraneous checks to make sure it was or wasn't on the main thread at any given moment. This seemed to be a vestige of the days before Grand Central Dispatch, which takes care of all of that. The other main breakthrough that I had tinkering around with things, was how efficient iPhone was at spawning new threads for network connections. By scheduling each NSURLConnection with its own run loop, a lot more network data could be handled in parallel. This can be seen in the example app: on a decent Wifi connection and an iPhone 4, you can scroll the tableView at nearly 60fps while each cell loads its own image.
There are a few other tricks I found, like replacing NSURLCache with NSCache for images, or the way subclasses of AFHTTPRequestOperation can easily build out behavior on top of the general NSURLRequest, NSHTTPURLResponse, NSData, NSError callback (see JSON and Image request operations).
So yeah: more explanation and documentation is on the way :)
3f901c8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool. Looking forward to hearing more.
Not surprised about NSCache being better than NSURLCache. I bet a lot of overhead is in disk writes + image decoding. Great find!