From 9ed38fd30a7e8d576f131a31154ce60729c22de5 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Wed, 15 Aug 2012 08:16:42 -0700 Subject: [PATCH] Initial implementation of NSCoding and NSCopying support --- AFNetworking/AFURLConnectionOperation.h | 2 +- AFNetworking/AFURLConnectionOperation.m | 61 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFURLConnectionOperation.h b/AFNetworking/AFURLConnectionOperation.h index 1aca1d1..6a17080 100644 --- a/AFNetworking/AFURLConnectionOperation.h +++ b/AFNetworking/AFURLConnectionOperation.h @@ -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 ///------------------------------- /// @name Accessing Run Loop Modes diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 9d5d95f..d291fb9 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -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