Initial implementation of NSCoding and NSCopying support

This commit is contained in:
Mattt Thompson 2012-08-15 08:16:42 -07:00
parent b3d63b58c0
commit 9ed38fd30a
2 changed files with 62 additions and 1 deletions

View file

@ -75,7 +75,7 @@ extern NSString * const AFNetworkingOperationDidFinishNotification;
@warning Attempting to load a `file://` URL in iOS 4 may result in an `NSInvalidArgumentException`, caused by the connection returning `NSURLResponse` rather than `NSHTTPURLResponse`, which is the behavior as of iOS 5.
*/
@interface AFURLConnectionOperation : NSOperation
@interface AFURLConnectionOperation : NSOperation <NSCoding, NSCopying>
///-------------------------------
/// @name Accessing Run Loop Modes

View file

@ -606,4 +606,65 @@ didReceiveResponse:(NSURLResponse *)response
}
}
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)aDecoder {
NSURLRequest *request = [aDecoder decodeObjectForKey:@"request"];
self = [self initWithRequest:request];
if (!self) {
return nil;
}
self.runLoopModes = [aDecoder decodeObjectForKey:@"runLoopModes"];
self.state = [aDecoder decodeIntegerForKey:@"state"];
self.cancelled = [aDecoder decodeBoolForKey:@"isCancelled"];
self.response = [aDecoder decodeObjectForKey:@"response"];
self.error = [aDecoder decodeObjectForKey:@"error"];
self.responseData = [aDecoder decodeObjectForKey:@"responseData"];
self.totalBytesRead = [[aDecoder decodeObjectForKey:@"totalBytesRead"] longLongValue];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[self pause];
[aCoder encodeObject:self.runLoopModes forKey:@"runLoopModes"];
[aCoder encodeObject:self.request forKey:@"request"];
switch (self.state) {
case AFHTTPOperationExecutingState:
case AFHTTPOperationPausedState:
[aCoder encodeInteger:AFHTTPOperationReadyState forKey:@"state"];
break;
default:
[aCoder encodeInteger:self.state forKey:@"state"];
break;
}
[aCoder encodeBool:[self isCancelled] forKey:@"isCancelled"];
[aCoder encodeObject:self.response forKey:@"response"];
[aCoder encodeObject:self.error forKey:@"error"];
[aCoder encodeObject:self.responseData forKey:@"responseData"];
[aCoder encodeObject:[NSNumber numberWithLongLong:self.totalBytesRead] forKey:@"totalBytesRead"];
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
AFURLConnectionOperation *operation = [[[self class] allocWithZone:zone] initWithRequest:self.request];
operation.runLoopModes = [self.runLoopModes copyWithZone:zone];
operation.uploadProgress = [self.uploadProgress copy];
operation.downloadProgress = [self.downloadProgress copy];
operation.authenticationAgainstProtectionSpace = [self.authenticationAgainstProtectionSpace copy];
operation.authenticationChallenge = [self.authenticationChallenge copy];
operation.cacheResponse = [self.cacheResponse copy];
operation.redirectResponse = [self.redirectResponse copy];
return operation;
}
@end