2011-10-04 00:13:12 -05:00
// AFURLConnectionOperation.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>
/**
2012-08-23 10:08:12 -07:00
Indicates an error occurred in AFNetworking .
2011-10-04 00:13:12 -05:00
@ discussion Error codes for AFNetworkingErrorDomain correspond to codes in NSURLErrorDomain .
*/
extern NSString * const AFNetworkingErrorDomain ;
/**
Posted when an operation begins executing .
*/
extern NSString * const AFNetworkingOperationDidStartNotification ;
/**
Posted when an operation finishes .
*/
extern NSString * const AFNetworkingOperationDidFinishNotification ;
2011-10-11 15:27:36 -05:00
/**
2011-10-12 10:42:53 -05:00
` AFURLConnectionOperation ` is an ` NSOperation ` that implements NSURLConnection delegate methods .
2011-10-11 15:27:36 -05:00
2011-10-11 16:48:10 -05:00
# # Subclassing Notes
2011-10-11 15:27:36 -05:00
This is the base class of all network request operations . You may wish to create your own subclass in order to implement additional ` NSURLConnection ` delegate methods ( see " `NSURLConnection` Delegate Methods " below ) , or to provide additional properties and / or class constructors .
2011-11-11 11:28:05 -06:00
If you are creating a subclass that communicates over the HTTP or HTTPS protocols , you may want to consider subclassing ` AFHTTPRequestOperation ` instead , as it supports specifying acceptable content types or status codes .
2011-10-11 15:27:36 -05:00
# # NSURLConnection Delegate Methods
` AFURLConnectionOperation ` implements the following ` NSURLConnection ` delegate methods :
- ` connection : didReceiveResponse : `
- ` connection : didReceiveData : `
- ` connectionDidFinishLoading : `
- ` connection : didFailWithError : `
- ` connection : didSendBodyData : totalBytesWritten : totalBytesExpectedToWrite : `
- ` connection : willCacheResponse : `
2012-03-13 09:04:59 -07:00
- ` connection : canAuthenticateAgainstProtectionSpace : `
- ` connection : didReceiveAuthenticationChallenge : `
2011-10-11 15:27:36 -05:00
2012-08-23 10:08:12 -07:00
If any of these methods are overridden in a subclass , they _must_ call the ` super ` implementation first .
2012-03-13 09:04:59 -07:00
2011-10-11 15:27:36 -05:00
# # Class Constructors
2011-10-12 10:42:53 -05:00
Class constructors , or methods that return an unowned ( zero retain count ) instance , are the preferred way for subclasses to encapsulate any particular logic for handling the setup or parsing of response data . For instance , ` AFJSONRequestOperation ` provides ` JSONRequestOperationWithRequest : success : failure : ` , which takes block arguments , whose parameter on for a successful request is the JSON object initialized from the ` response data ` .
2011-10-11 15:27:36 -05:00
# # Callbacks and Completion Blocks
2011-10-12 10:42:53 -05:00
The built - in ` completionBlock ` provided by ` NSOperation ` allows for custom behavior to be executed after the request finishes . It is a common pattern for class constructors in subclasses to take callback block parameters , and execute them conditionally in the body of its ` completionBlock ` . Make sure to handle cancelled operations appropriately when setting a ` completionBlock ` ( e . g . returning early before parsing response data ) . See the implementation of any of the ` AFHTTPRequestOperation ` subclasses for an example of this .
2011-10-11 15:27:36 -05:00
2012-03-09 12:56:10 -08:00
@ warning Subclasses are strongly discouraged from overriding ` setCompletionBlock : ` , as ` AFURLConnectionOperation ` ' s implementation includes a workaround to mitigate retain cycles , and what Apple rather ominously refers to as " The Deallocation Problem " ( See http : //developer.apple.com/library/ios/technotes/tn2109/_index.html#//apple_ref/doc/uid/DTS40010274-CH1-SUBSECTION11)
2012-08-15 09:59:00 -07:00
# # NSCoding & NSCopying Conformance
` AFURLConnectionOperation ` conforms to the ` NSCoding ` and ` NSCopying ` protocols , allowing operations to be archived to disk , and copied in memory , respectively . However , because of the intrinsic limitations of capturing the exact state of an operation at a particular moment , there are some important caveats to keep in mind :
# ## NSCoding Caveats
2012-08-15 10:03:46 -07:00
- Encoded operations do not include any block or stream properties . Be sure to set ` completionBlock ` , ` outputStream ` , and any callback blocks as necessary when using ` - initWithCoder : ` or ` NSKeyedUnarchiver ` .
2012-08-15 09:59:00 -07:00
- Operations are paused on ` encodeWithCoder : ` . If the operation was encoded while paused or still executing , its archived state will return ` YES ` for ` isReady ` . Otherwise , the state of an operation when encoding will remain unchanged .
# ## NSCopying Caveats
- ` - copy ` and ` - copyWithZone : ` return a new operation with the ` NSURLRequest ` of the original . So rather than an exact copy of the operation at that particular instant , the copying mechanism returns a completely new instance , which can be useful for retrying operations .
2012-08-15 10:03:46 -07:00
- A copy of an operation will not include the ` outputStream ` of the original .
2012-08-22 11:46:03 -07:00
- Operation copies do not include ` completionBlock ` . ` completionBlock ` often strongly captures a reference to ` self ` , which , perhaps surprisingly , would otherwise point to the _original_ operation when copied .
2011-10-11 15:27:36 -05:00
*/
2012-08-15 08:16:42 -07:00
@ interface AFURLConnectionOperation : NSOperation < NSCoding , NSCopying >
2011-10-04 00:13:12 -05:00
2011-10-11 15:27:36 -05:00
///-------------------------------
/// @name Accessing Run Loop Modes
///-------------------------------
/**
The run loop modes in which the operation will run on the network thread . By default , this is a single - member set containing ` NSRunLoopCommonModes ` .
*/
2011-10-04 00:13:12 -05:00
@ property ( nonatomic , retain ) NSSet * runLoopModes ;
2011-10-11 15:27:36 -05:00
///-----------------------------------------
/// @name Getting URL Connection Information
///-----------------------------------------
/**
The request used by the operation ' s connection .
*/
2011-10-04 00:13:12 -05:00
@ property ( readonly , nonatomic , retain ) NSURLRequest * request ;
2011-10-11 15:27:36 -05:00
/**
The last response received by the operation ' s connection .
*/
2011-10-04 00:13:12 -05:00
@ property ( readonly , nonatomic , retain ) NSURLResponse * response ;
2011-10-11 15:27:36 -05:00
/**
2012-08-23 10:08:12 -07:00
The error , if any , that occurred in the lifecycle of the request .
2011-10-11 15:27:36 -05:00
*/
2011-10-04 00:13:12 -05:00
@ property ( readonly , nonatomic , retain ) NSError * error ;
2011-10-11 15:27:36 -05:00
///----------------------------
/// @name Getting Response Data
///----------------------------
/**
The data received during the request .
*/
2011-10-05 12:36:45 -05:00
@ property ( readonly , nonatomic , retain ) NSData * responseData ;
2011-10-11 15:27:36 -05:00
/**
The string representation of the response data .
2011-10-13 12:57:39 -05:00
@ discussion This method uses the string encoding of the response , or if UTF - 8 if not specified , to construct a string from the response data .
2011-10-11 15:27:36 -05:00
*/
2011-10-04 00:13:12 -05:00
@ property ( readonly , nonatomic , copy ) NSString * responseString ;
2011-10-11 15:27:36 -05:00
///------------------------
/// @name Accessing Streams
///------------------------
/**
The input stream used to read data to be sent during the request .
@ discussion This property acts as a proxy to the ` HTTPBodyStream ` property of ` request ` .
*/
2011-10-04 00:13:12 -05:00
@ property ( nonatomic , retain ) NSInputStream * inputStream ;
2011-10-11 15:27:36 -05:00
/**
The output stream that is used to write data received until the request is finished .
2012-03-27 11:01:20 -07:00
@ discussion By default , data is accumulated into a buffer that is stored into ` responseData ` upon completion of the request . When ` outputStream ` is set , the data will not be accumulated into an internal buffer , and as a result , the ` responseData ` property of the completed request will be ` nil ` . The output stream will be scheduled in the network thread runloop upon being set .
2011-10-11 15:27:36 -05:00
*/
2011-10-04 00:13:12 -05:00
@ property ( nonatomic , retain ) NSOutputStream * outputStream ;
2011-10-11 15:27:36 -05:00
///------------------------------------------------------
/// @name Initializing an AFURLConnectionOperation Object
///------------------------------------------------------
2011-10-04 00:13:12 -05:00
/**
2011-10-11 15:27:36 -05:00
Initializes and returns a newly allocated operation object with a url connection configured with the specified url request .
@ param urlRequest The request object to be used by the operation connection .
2011-10-04 00:13:12 -05:00
@ discussion This is the designated initializer .
*/
- ( id ) initWithRequest : ( NSURLRequest * ) urlRequest ;
2012-03-27 12:05:00 -07:00
///----------------------------------
/// @name Pausing / Resuming Requests
///----------------------------------
2012-04-24 21:28:06 -07:00
/**
Pauses the execution of the request operation .
2012-07-23 16:22:48 -07:00
@ discussion A paused operation returns ` NO ` for ` - isReady ` , ` - isExecuting ` , and ` - isFinished ` . As such , it will remain in an ` NSOperationQueue ` until it is either cancelled or resumed . Pausing a finished , cancelled , or paused operation has no effect .
2012-04-24 21:28:06 -07:00
*/
2012-03-27 11:01:20 -07:00
- ( void ) pause ;
2012-04-24 21:28:06 -07:00
/**
Whether the request operation is currently paused .
@ return ` YES ` if the operation is currently paused , otherwise ` NO ` .
*/
2012-03-27 11:01:20 -07:00
- ( BOOL ) isPaused ;
2012-04-24 21:28:06 -07:00
/**
Resumes the execution of the paused request operation .
@ discussion Pause / Resume behavior varies depending on the underlying implementation for the operation class . In its base implementation , resuming a paused requests restarts the original request . However , since HTTP defines a specification for how to request a specific content range , ` AFHTTPRequestOperation ` will resume downloading the request from where it left off , instead of restarting the original request .
*/
2012-03-27 11:01:20 -07:00
- ( void ) resume ;
2012-03-20 11:31:18 -07:00
///----------------------------------------------
/// @name Configuring Backgrounding Task Behavior
///----------------------------------------------
/**
Specifies that the operation should continue execution after the app has entered the background , and the expiration handler for that background task .
@ param handler A handler to be called shortly before the application ’ s remaining background time reaches 0. The handler is wrapped in a block that cancels the operation , and cleans up and marks the end of execution , unlike the ` handler ` parameter in ` UIApplication - beginBackgroundTaskWithExpirationHandler : ` , which expects this to be done in the handler itself . The handler is called synchronously on the main thread , thus blocking the application ’ s suspension momentarily while the application is notified .
*/
# if __IPHONE_OS_VERSION_MIN_REQUIRED
- ( void ) setShouldExecuteAsBackgroundTaskWithExpirationHandler : ( void ( ^ ) ( void ) ) handler ;
# endif
2011-10-04 00:13:12 -05:00
///---------------------------------
/// @name Setting Progress Callbacks
///---------------------------------
/**
2012-03-15 17:20:56 -07:00
Sets a callback to be called when an undetermined number of bytes have been uploaded to the server .
2011-10-04 00:13:12 -05:00
2012-08-04 15:34:09 -04:00
@ param block A block object to be called when an undetermined number of bytes have been uploaded to the server . This block has no return value and takes three arguments : the number of bytes written since the last time the upload progress block was called , the total bytes written , and the total bytes expected to be written during the request , as initially determined by the length of the HTTP body . This block may be called multiple times , and will execute on the main thread .
@ discussion This block is called on the main thread .
2011-10-04 00:13:12 -05:00
@ see setDownloadProgressBlock
*/
2012-08-23 13:15:47 -05:00
- ( void ) setUploadProgressBlock : ( void ( ^ ) ( NSUInteger bytesWritten , long long totalBytesWritten , long long totalBytesExpectedToWrite ) ) block ;
2011-10-04 00:13:12 -05:00
/**
2012-03-15 17:20:56 -07:00
Sets a callback to be called when an undetermined number of bytes have been downloaded from the server .
2011-10-04 00:13:12 -05:00
2012-08-04 15:34:09 -04:00
@ param block A block object to be called when an undetermined number of bytes have been downloaded from the server . This block has no return value and takes three arguments : the number of bytes read since the last time the download progress block was called , the total bytes read , and the total bytes expected to be read during the request , as initially determined by the expected content size of the ` NSHTTPURLResponse ` object . This block may be called multiple times , and will execute on the main thread .
2011-10-04 00:13:12 -05:00
@ see setUploadProgressBlock
*/
2012-08-23 13:15:47 -05:00
- ( void ) setDownloadProgressBlock : ( void ( ^ ) ( NSUInteger bytesRead , long long totalBytesRead , long long totalBytesExpectedToRead ) ) block ;
2011-10-04 00:13:12 -05:00
2011-12-04 03:45:02 -06:00
///-------------------------------------------------
2012-03-09 13:50:46 -08:00
/// @name Setting NSURLConnection Delegate Callbacks
2011-12-04 03:45:02 -06:00
///-------------------------------------------------
2012-02-14 08:38:49 -08:00
/**
Sets a block to be executed to determine whether the connection should be able to respond to a protection space ' s form of authentication , as handled by the ` NSURLConnectionDelegate ` method ` connection : canAuthenticateAgainstProtectionSpace : ` .
@ param block A block object to be executed to determine whether the connection should be able to respond to a protection space ' s form of authentication . The block has a ` BOOL ` return type and takes two arguments : the URL connection object , and the protection space to authenticate against .
@ discussion If ` _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ ` is defined , ` connection : canAuthenticateAgainstProtectionSpace : ` will accept invalid SSL certificates , returning ` YES ` if the protection space authentication method is ` NSURLAuthenticationMethodServerTrust ` .
*/
- ( void ) setAuthenticationAgainstProtectionSpaceBlock : ( BOOL ( ^ ) ( NSURLConnection * connection , NSURLProtectionSpace * protectionSpace ) ) block ;
/**
Sets a block to be executed when the connection must authenticate a challenge in order to download its request , as handled by the ` NSURLConnectionDelegate ` method ` connection : didReceiveAuthenticationChallenge : ` .
@ param block A block object to be executed when the connection must authenticate a challenge in order to download its request . The block has no return type and takes two arguments : the URL connection object , and the challenge that must be authenticated .
@ discussion If ` _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ ` is defined , ` connection : didReceiveAuthenticationChallenge : ` will attempt to have the challenge sender use credentials with invalid SSL certificates .
*/
2011-12-04 03:45:02 -06:00
- ( void ) setAuthenticationChallengeBlock : ( void ( ^ ) ( NSURLConnection * connection , NSURLAuthenticationChallenge * challenge ) ) block ;
2012-03-09 13:50:46 -08:00
/**
2012-05-25 15:03:19 -07:00
Sets a block to be executed when the server redirects the request from one URL to another URL , or when the request URL changed by the ` NSURLProtocol ` subclass handling the request in order to standardize its format , as handled by the ` NSURLConnectionDelegate ` method ` connection : willSendRequest : redirectResponse : ` .
2012-03-09 13:50:46 -08:00
2012-05-25 15:03:19 -07:00
@ param block A block object to be executed when the request URL was changed . The block returns an ` NSURLRequest ` object , the URL request to redirect , and takes three arguments : the URL connection object , the the proposed redirected request , and the URL response that caused the redirect .
2012-03-09 13:50:46 -08:00
*/
2012-05-25 15:03:19 -07:00
- ( void ) setRedirectResponseBlock : ( NSURLRequest * ( ^ ) ( NSURLConnection * connection , NSURLRequest * request , NSURLResponse * redirectResponse ) ) block ;
2012-03-09 13:50:46 -08:00
2012-05-24 14:45:01 -05:00
/**
2012-05-25 15:03:19 -07:00
Sets a block to be executed to modify the response a connection will cache , if any , as handled by the ` NSURLConnectionDelegate ` method ` connection : willCacheResponse : ` .
2012-05-24 14:45:01 -05:00
2012-05-25 15:03:19 -07:00
@ param block A block object to be executed to determine what response a connection will cache , if any . The block returns an ` NSCachedURLResponse ` object , the cached response to store in memory or ` nil ` to prevent the response from being cached , and takes two arguments : the URL connection object , and the cached response provided for the request .
2012-05-24 14:45:01 -05:00
*/
2012-05-25 15:03:19 -07:00
- ( void ) setCacheResponseBlock : ( NSCachedURLResponse * ( ^ ) ( NSURLConnection * connection , NSCachedURLResponse * cachedResponse ) ) block ;
2012-05-24 14:45:01 -05:00
2012-08-04 15:48:01 -04:00
///---------------------------------------
/// @name NSURLConnection Delegate Methods
/// @discussion NSURLConnection delegate methods were part of an informal protocol until iOS 5 & Mac OS 10.7, so the method signatures are declared here in order to allow subclasses to override these methods and call back to the super implementation.
///---------------------------------------
- ( BOOL ) connection : ( NSURLConnection * ) connection
canAuthenticateAgainstProtectionSpace : ( NSURLProtectionSpace * ) protectionSpace ;
- ( void ) connection : ( NSURLConnection * ) connection
didReceiveAuthenticationChallenge : ( NSURLAuthenticationChallenge * ) challenge ;
- ( NSURLRequest * ) connection : ( NSURLConnection * ) connection
willSendRequest : ( NSURLRequest * ) request
redirectResponse : ( NSURLResponse * ) redirectResponse ;
- ( void ) connection : ( NSURLConnection * ) connection
didSendBodyData : ( NSInteger ) bytesWritten
totalBytesWritten : ( NSInteger ) totalBytesWritten
totalBytesExpectedToWrite : ( NSInteger ) totalBytesExpectedToWrite ;
- ( void ) connection : ( NSURLConnection * ) connection
didReceiveResponse : ( NSURLResponse * ) response ;
- ( void ) connection : ( NSURLConnection * ) connection
didReceiveData : ( NSData * ) data ;
- ( void ) connectionDidFinishLoading : ( NSURLConnection * ) connection ;
- ( void ) connection : ( NSURLConnection * ) connection
didFailWithError : ( NSError * ) error ;
- ( NSCachedURLResponse * ) connection : ( NSURLConnection * ) connection
willCacheResponse : ( NSCachedURLResponse * ) cachedResponse ;
2011-11-11 11:28:05 -06:00
@ end