From b701559a49ed8fcb4d701bb6412ee9e1539f1fc2 Mon Sep 17 00:00:00 2001 From: Mugunth Date: Mon, 5 Dec 2011 16:16:13 +0800 Subject: [PATCH 1/2] Fixed image caching on adhoc basis --- MKNetworkKit/MKNetworkEngine.m | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/MKNetworkKit/MKNetworkEngine.m b/MKNetworkKit/MKNetworkEngine.m index b743285..43cf1de 100644 --- a/MKNetworkKit/MKNetworkEngine.m +++ b/MKNetworkKit/MKNetworkEngine.m @@ -336,7 +336,15 @@ - (void)imageAtURL:(NSURL *)url onCompletion:(MKNKImageBlock) imageFetchedBlock DLog(@"%@", error); }]; - [self enqueueOperation:op]; + NSData *cachedData = [self cachedDataForOperation:op]; + + if(cachedData) { + [op setCachedData:cachedData]; + } + + if(0) { // If the cache is old + [self enqueueOperation:op]; + } } #pragma - From 190828468265678b28b3e32e8035cd5fef490b40 Mon Sep 17 00:00:00 2001 From: Mugunth Date: Mon, 5 Dec 2011 16:39:39 +0800 Subject: [PATCH 2/2] Network Kit Caching fixes --- MKNetworkKit/MKNetworkEngine.m | 2 +- MKNetworkKit/MKNetworkOperation.m | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/MKNetworkKit/MKNetworkEngine.m b/MKNetworkKit/MKNetworkEngine.m index 43cf1de..90b23a8 100644 --- a/MKNetworkKit/MKNetworkEngine.m +++ b/MKNetworkKit/MKNetworkEngine.m @@ -342,7 +342,7 @@ - (void)imageAtURL:(NSURL *)url onCompletion:(MKNKImageBlock) imageFetchedBlock [op setCachedData:cachedData]; } - if(0) { // If the cache is old + if(!cachedData) { // If the cache is old when caching is implemented, this should come from cache [self enqueueOperation:op]; } } diff --git a/MKNetworkKit/MKNetworkOperation.m b/MKNetworkKit/MKNetworkOperation.m index 6676ece..5fa0f3a 100644 --- a/MKNetworkKit/MKNetworkOperation.m +++ b/MKNetworkKit/MKNetworkOperation.m @@ -173,9 +173,7 @@ -(BOOL) isEqual:(id)object { -(NSString*) uniqueIdentifier { - NSString *str = [NSString stringWithFormat:@"%@ %@", - self.request.HTTPMethod, - [self.request.URL absoluteString]]; + NSString *str = [self curlCommandLineString]; if(self.username || self.password) { @@ -422,14 +420,18 @@ - (id)initWithURLString:(NSString *)aURLString NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding)); - [self.request addValue: - [NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] - forHTTPHeaderField:@"Content-Type"]; - [self.request addValue:[NSString stringWithFormat:@"%@, en-us", [[NSLocale preferredLanguages] componentsJoinedByString:@", "] ] forHTTPHeaderField:@"Accept-Language"]; + if (([method isEqualToString:@"POST"] || + [method isEqualToString:@"PUT"]) && (params && [params count] > 0)) { + + [self.request addValue: + [NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] + forHTTPHeaderField:@"Content-Type"]; + } + self.state = MKNetworkOperationStateReady; } @@ -449,8 +451,10 @@ -(void) addHeaders:(NSDictionary*) headersDictionary { -(NSString*) description { - NSMutableString *displayString = [[self curlCommandLineString] mutableCopy]; - + NSMutableString *displayString = [NSMutableString stringWithFormat:@"%@\nRequest\n-------\n%@", + [[NSDate date] descriptionWithLocale:[NSLocale currentLocale]], + [self curlCommandLineString]]; + NSString *responseString = [self responseString]; if([responseString length] > 0) { [displayString appendFormat:@"\n--------\nResponse\n--------\n%@\n", responseString]; @@ -461,9 +465,7 @@ -(NSString*) description { -(NSString*) curlCommandLineString { - __block NSMutableString *displayString = [NSMutableString stringWithFormat:@"%@\nRequest\n-------\ncurl -X %@", - [[NSDate date] descriptionWithLocale:[NSLocale currentLocale]], - self.request.HTTPMethod]; + __block NSMutableString *displayString = [NSMutableString stringWithFormat:@"curl -X %@", self.request.HTTPMethod]; if([self.filesToBePosted count] == 0 && [self.dataToBePosted count] == 0) { [[self.request allHTTPHeaderFields] enumerateKeysAndObjectsUsingBlock:^(id key, id val, BOOL *stop)