Adding NSCoding and NSCopying conformance to AFHTTPClient

This commit is contained in:
Mattt Thompson 2012-08-15 10:22:07 -07:00
parent e8c8367e99
commit c7df7eacd5
2 changed files with 47 additions and 1 deletions

View file

@ -124,8 +124,14 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/ [NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/ [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
## NSCoding / NSCopying Conformance
`AFHTTPClient` conforms to the `NSCoding` and `NSCopying` protocols, allowing operations to be archived to disk, and copied in memory, respectively. There are a few minor caveats to keep in mind, however:
- Archives and copies of HTTP clients will be initialized with an empty operation queue.
- NSCoding cannot serialize / deserialize block properties, so an archive of an HTTP client will not include any reachability callback block that may be set.
*/ */
@interface AFHTTPClient : NSObject @interface AFHTTPClient : NSObject <NSCoding, NSCopying>
///--------------------------------------- ///---------------------------------------
/// @name Accessing HTTP Client Properties /// @name Accessing HTTP Client Properties

View file

@ -664,6 +664,46 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
[self enqueueHTTPRequestOperation:operation]; [self enqueueHTTPRequestOperation:operation];
} }
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)aDecoder {
NSURL *baseURL = [aDecoder decodeObjectForKey:@"baseURL"];
self = [self initWithBaseURL:baseURL];
if (!self) {
return nil;
}
self.stringEncoding = [aDecoder decodeIntegerForKey:@"stringEncoding"];
self.parameterEncoding = [aDecoder decodeIntegerForKey:@"parameterEncoding"];
self.registeredHTTPOperationClassNames = [aDecoder decodeObjectForKey:@"registeredHTTPOperationClassNames"];
self.defaultHeaders = [aDecoder decodeObjectForKey:@"defaultHeaders"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.baseURL forKey:@"baseURL"];
[aCoder encodeInteger:self.stringEncoding forKey:@"stringEncoding"];
[aCoder encodeInteger:self.parameterEncoding forKey:@"parameterEncoding"];
[aCoder encodeObject:self.registeredHTTPOperationClassNames forKey:@"registeredHTTPOperationClassNames"];
[aCoder encodeObject:self.defaultHeaders forKey:@"defaultHeaders"];
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
AFHTTPClient *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
HTTPClient.stringEncoding = self.stringEncoding;
HTTPClient.parameterEncoding = self.parameterEncoding;
HTTPClient.registeredHTTPOperationClassNames = [[self.registeredHTTPOperationClassNames copyWithZone:zone] autorelease];
HTTPClient.defaultHeaders = [[self.defaultHeaders copyWithZone:zone] autorelease];
HTTPClient.networkReachabilityStatusBlock = self.networkReachabilityStatusBlock;
return HTTPClient;
}
@end @end
#pragma mark - #pragma mark -