Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Significant performance improvments and API redesign
Browse files Browse the repository at this point in the history
Removing Q*Operation dependency
  • Loading branch information
mattt committed Aug 3, 2011
2 parents ba98a2a + 3a4392e commit 3f901c8
Show file tree
Hide file tree
Showing 30 changed files with 10,314 additions and 7,695 deletions.
62 changes: 0 additions & 62 deletions AFNetworking/AFHTTPOperation.h

This file was deleted.

106 changes: 0 additions & 106 deletions AFNetworking/AFHTTPOperation.m

This file was deleted.

56 changes: 56 additions & 0 deletions AFNetworking/AFHTTPRequestOperation.h
@@ -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

3 comments on commit 3f901c8

@adamjernst
Copy link
Contributor

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?

@mattt
Copy link
Contributor Author

@mattt mattt commented on 3f901c8 Aug 4, 2011

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 :)

@adamjernst
Copy link
Contributor

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!

Please sign in to comment.