Browse Source

Removing AFURLCache, using NSURLCache instead

Mattt Thompson 14 years ago
parent
commit
3f851b0a23

+ 1 - 2
AFNetworking/AFImageRequestOperation.m

@@ -21,7 +21,6 @@
 // THE SOFTWARE.
 
 #import "AFImageRequestOperation.h"
-#import "AFURLCache.h"
 
 #import "UIImage+AFNetworking.h"
 
@@ -119,7 +118,7 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
         }
         NSURLResponse *response = [[[NSURLResponse alloc] initWithURL:[self.lastRequest URL] MIMEType:[self.lastResponse MIMEType] expectedContentLength:[processedImageData length] textEncodingName:[self.lastResponse textEncodingName]] autorelease];
         NSCachedURLResponse *cachedResponse = [[[NSCachedURLResponse alloc] initWithResponse:response data:processedImageData] autorelease];
-        [[AFURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:self.lastRequest];
+        [[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:self.lastRequest];
         [pool drain];
     }
 }

+ 0 - 2
AFNetworking/AFRestClient.h

@@ -30,8 +30,6 @@
 @end
 
 @interface AFRestClient : NSObject <AFRestClient>
-+ (id)sharedClient;
-
 - (NSString *)defaultValueForHeader:(NSString *)header;
 - (void)setDefaultHeader:(NSString *)header value:(NSString *)value;
 - (void)setAuthorizationHeaderWithToken:(NSString *)token;

+ 0 - 11
AFNetworking/AFRestClient.m

@@ -24,7 +24,6 @@
 #import "AFHTTPOperation.h"
 
 static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding;
-static AFRestClient *_sharedClient = nil;
 
 @interface AFRestClient ()
 @property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders;
@@ -35,16 +34,6 @@ static AFRestClient *_sharedClient = nil;
 @synthesize defaultHeaders = _defaultHeaders;
 @synthesize operationQueue = _operationQueue;
 
-+ (id)sharedClient {
-    if (_sharedClient == nil) {
-        @synchronized(self) {
-            _sharedClient = [[self alloc] init];
-        }
-    }
-    
-    return _sharedClient;
-}
-
 - (id)init {
     self = [super init];
     if (!self) {

+ 0 - 39
AFNetworking/AFURLCache.h

@@ -1,39 +0,0 @@
-// AFURLCache.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>
-
-extern NSString * AFURLCacheKeyForNSURLRequest(NSURLRequest *request);
-
-@interface AFURLCache : NSURLCache {
-@private
-	NSCountedSet *_cachedRequests;
-	NSMutableDictionary *_keyedCachedResponsesByRequest;
-	
-	NSOperationQueue *_periodicMaintenanceOperationQueue;
-    NSOperation *_periodicMaintenanceOperation;
-	NSTimer *_periodicMaintenanceTimer;
-}
-
-+ (NSString *)defaultCachePath;
-
-@end

+ 0 - 159
AFNetworking/AFURLCache.m

@@ -1,159 +0,0 @@
-// AFURLCache.m
-//
-// 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 <CommonCrypto/CommonDigest.h>
-#import "AFURLCache.h"
-
-static const NSTimeInterval kAFURLCacheMaintenanceTimeInterval = 30.0;
-
-NSString * AFURLCacheKeyForNSURLRequest(NSURLRequest *request) {
-    const char *str = [[[request URL] absoluteString] UTF8String];
-    unsigned char r[CC_MD5_DIGEST_LENGTH];
-    CC_MD5(str, strlen(str), r);
-    return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
-            r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
-}
-
-@interface AFURLCache () 
-@property (nonatomic, retain) NSCountedSet *cachedRequests;
-@property (nonatomic, retain) NSMutableDictionary *keyedCachedResponsesByRequest;
-
-@property (nonatomic, retain) NSOperationQueue *periodicMaintenanceOperationQueue;
-@property (nonatomic, retain) NSOperation *periodicMaintenanceOperation;
-@property (nonatomic, retain) NSTimer *periodicMaintenanceTimer;
-@end
-
-@implementation AFURLCache
-@synthesize cachedRequests = _cachedRequests;
-@synthesize keyedCachedResponsesByRequest = _keyedCachedResponsesByRequest;
-@synthesize periodicMaintenanceOperationQueue = _periodicMaintenanceOperationQueue;
-@synthesize periodicMaintenanceOperation = _periodicMaintenanceOperation;
-@synthesize periodicMaintenanceTimer = _periodicMaintenanceTimer;
-
-+ (NSString *)defaultCachePath {
-    return nil;
-}
-
-- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path {
-	self = [super initWithMemoryCapacity:memoryCapacity diskCapacity:diskCapacity diskPath:path];
-	if (!self) {
-		return nil;
-	}
-	
-	self.cachedRequests = [NSCountedSet setWithCapacity:200];
-	self.keyedCachedResponsesByRequest = [NSMutableDictionary dictionaryWithCapacity:200];
-	
-	self.periodicMaintenanceOperationQueue = [[[NSOperationQueue alloc] init] autorelease];
-	[self.periodicMaintenanceOperationQueue setMaxConcurrentOperationCount:1];
-	self.periodicMaintenanceTimer = [[NSTimer scheduledTimerWithTimeInterval:kAFURLCacheMaintenanceTimeInterval target:self selector:@selector(periodicMaintenance) userInfo:nil repeats:YES] retain];
-	
-	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sweepMemoryCache) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
-	
-	return self;
-}
-
-- (void)dealloc {
-	[[NSNotificationCenter defaultCenter] removeObserver:self];
-	
-	[_cachedRequests release];
-	[_keyedCachedResponsesByRequest release];
-	
-	[_periodicMaintenanceOperationQueue cancelAllOperations];
-	[_periodicMaintenanceOperationQueue release];
-	[_periodicMaintenanceOperation release];
-	
-    [_periodicMaintenanceTimer invalidate];
-	_periodicMaintenanceTimer = nil;
-	[super dealloc];
-}
-
-#pragma mark - NSURLCache
-
-- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
-    NSString *cacheKey = AFURLCacheKeyForNSURLRequest(request);
-	NSCachedURLResponse *cachedResponse = [self.keyedCachedResponsesByRequest valueForKey:cacheKey];
-	if (cachedResponse) {
-		return cachedResponse;
-	}
-	
-	return nil;
-}
-
-- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request {
-	NSString *cacheKey = AFURLCacheKeyForNSURLRequest(request);
-	[self.keyedCachedResponsesByRequest setObject:cachedResponse forKey:cacheKey];
-    [self.cachedRequests addObject:cacheKey];
-}
-
-- (void)removeCachedResponseForRequest:(NSURLRequest *)request {
-    NSString *cacheKey = AFURLCacheKeyForNSURLRequest(request);
-	[self.keyedCachedResponsesByRequest removeObjectForKey:cacheKey];
-	[self.cachedRequests removeObject:cacheKey];
-}
-
-- (void)removeAllCachedResponses {
-	[self.keyedCachedResponsesByRequest removeAllObjects];
-    [self.cachedRequests removeAllObjects];
-}
-
-- (NSUInteger)currentMemoryUsage {
-	return [[[self.keyedCachedResponsesByRequest allValues] valueForKeyPath:@"@sum.data.length"] integerValue];
-}
-
-#pragma mark - Maintenance
-
-- (void)periodicMaintenance {
-    [self.periodicMaintenanceOperation cancel];
-    self.periodicMaintenanceOperation = nil;
-    if ([self currentMemoryUsage] > [self memoryCapacity]) {
-        self.periodicMaintenanceOperation = [[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sweepMemoryCache) object:nil] autorelease];
-        [self.periodicMaintenanceOperationQueue addOperation:self.periodicMaintenanceOperation];
-    }
-}
-
-- (void)sweepMemoryCache {
-	NSArray *sortedCachedRequests = [[self.cachedRequests allObjects] sortedArrayUsingComparator:^(id obj1, id obj2) { 
-		NSUInteger count1 = [self.cachedRequests countForObject:obj1];
-		NSUInteger count2 = [self.cachedRequests countForObject:obj2];
-		
-		if (count1 > count2) {
-			return NSOrderedDescending;
-		} else if (count1 < count2) {
-			return NSOrderedAscending;
-		} else {
-			return NSOrderedSame;
-		}
-	}];
-	
-	NSUInteger memoryDeficit = [self currentMemoryUsage] - [self memoryCapacity];
-	NSString *cacheKey = nil;
-	NSEnumerator *enumerator = [sortedCachedRequests reverseObjectEnumerator];
-	while (memoryDeficit > 0 && (cacheKey = [enumerator nextObject])) {
-		NSCachedURLResponse *response = (NSCachedURLResponse *)[self.keyedCachedResponsesByRequest objectForKey:cacheKey];
-		memoryDeficit -= [[response data] length];
-		[self.keyedCachedResponsesByRequest removeObjectForKey:cacheKey];
-	}
-	
-	self.cachedRequests = [NSCountedSet setWithArray:[self.keyedCachedResponsesByRequest allKeys]];	
-}
-
-@end

+ 0 - 6
Example/AFNetworking Example.xcodeproj/project.pbxproj

@@ -18,7 +18,6 @@
 		F8DA09DA1396ABED0057D0CC /* AFHTTPOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CF11396A98600CF3BD6 /* AFHTTPOperation.m */; };
 		F8DA09DB1396ABED0057D0CC /* AFImageRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CF21396A98600CF3BD6 /* AFImageRequestOperation.m */; };
 		F8DA09DC1396ABED0057D0CC /* AFRestClient.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CF31396A98600CF3BD6 /* AFRestClient.m */; };
-		F8DA09DD1396ABED0057D0CC /* AFURLCache.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CF41396A98600CF3BD6 /* AFURLCache.m */; };
 		F8DA09DE1396ABED0057D0CC /* UIImage+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CF51396A98600CF3BD6 /* UIImage+AFNetworking.m */; };
 		F8DA09DF1396ABED0057D0CC /* QHTTPOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25D0A1396A9A900CF3BD6 /* QHTTPOperation.m */; };
 		F8DA09E01396ABED0057D0CC /* QRunLoopOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25D0C1396A9A900CF3BD6 /* QRunLoopOperation.m */; };
@@ -37,13 +36,11 @@
 		F8D25CEB1396A98600CF3BD6 /* AFHTTPOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFHTTPOperation.h; path = ../AFNetworking/AFHTTPOperation.h; sourceTree = "<group>"; };
 		F8D25CEC1396A98600CF3BD6 /* AFImageRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFImageRequestOperation.h; path = ../AFNetworking/AFImageRequestOperation.h; sourceTree = "<group>"; };
 		F8D25CED1396A98600CF3BD6 /* AFRestClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFRestClient.h; path = ../AFNetworking/AFRestClient.h; sourceTree = "<group>"; };
-		F8D25CEE1396A98600CF3BD6 /* AFURLCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFURLCache.h; path = ../AFNetworking/AFURLCache.h; sourceTree = "<group>"; };
 		F8D25CEF1396A98600CF3BD6 /* UIImage+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+AFNetworking.h"; path = "../AFNetworking/UIImage+AFNetworking.h"; sourceTree = "<group>"; };
 		F8D25CF01396A98600CF3BD6 /* AFCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFCallback.m; path = ../AFNetworking/AFCallback.m; sourceTree = "<group>"; };
 		F8D25CF11396A98600CF3BD6 /* AFHTTPOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFHTTPOperation.m; path = ../AFNetworking/AFHTTPOperation.m; sourceTree = "<group>"; };
 		F8D25CF21396A98600CF3BD6 /* AFImageRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFImageRequestOperation.m; path = ../AFNetworking/AFImageRequestOperation.m; sourceTree = "<group>"; };
 		F8D25CF31396A98600CF3BD6 /* AFRestClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFRestClient.m; path = ../AFNetworking/AFRestClient.m; sourceTree = "<group>"; };
-		F8D25CF41396A98600CF3BD6 /* AFURLCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFURLCache.m; path = ../AFNetworking/AFURLCache.m; sourceTree = "<group>"; };
 		F8D25CF51396A98600CF3BD6 /* UIImage+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+AFNetworking.m"; path = "../AFNetworking/UIImage+AFNetworking.m"; sourceTree = "<group>"; };
 		F8D25D091396A9A900CF3BD6 /* QHTTPOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QHTTPOperation.h; sourceTree = "<group>"; };
 		F8D25D0A1396A9A900CF3BD6 /* QHTTPOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QHTTPOperation.m; sourceTree = "<group>"; };
@@ -237,8 +234,6 @@
 				F8D25CF21396A98600CF3BD6 /* AFImageRequestOperation.m */,
 				F8D25CED1396A98600CF3BD6 /* AFRestClient.h */,
 				F8D25CF31396A98600CF3BD6 /* AFRestClient.m */,
-				F8D25CEE1396A98600CF3BD6 /* AFURLCache.h */,
-				F8D25CF41396A98600CF3BD6 /* AFURLCache.m */,
 				F8D25CEF1396A98600CF3BD6 /* UIImage+AFNetworking.h */,
 				F8D25CF51396A98600CF3BD6 /* UIImage+AFNetworking.m */,
 			);
@@ -337,7 +332,6 @@
 				F8DA09DA1396ABED0057D0CC /* AFHTTPOperation.m in Sources */,
 				F8DA09DB1396ABED0057D0CC /* AFImageRequestOperation.m in Sources */,
 				F8DA09DC1396ABED0057D0CC /* AFRestClient.m in Sources */,
-				F8DA09DD1396ABED0057D0CC /* AFURLCache.m in Sources */,
 				F8DA09DE1396ABED0057D0CC /* UIImage+AFNetworking.m in Sources */,
 				F8DA09DF1396ABED0057D0CC /* QHTTPOperation.m in Sources */,
 				F8DA09E01396ABED0057D0CC /* QRunLoopOperation.m in Sources */,

File diff suppressed because it is too large
+ 162 - 259
Example/AFNetworking Example.xcodeproj/project.xcworkspace/xcuserdata/mattt.xcuserdatad/UserInterfaceState.xcuserstate


+ 2 - 2
Example/AppDelegate.m

@@ -19,16 +19,16 @@
 // 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 "AppDelegate.h"
 #import "NearbySpotsViewController.h"
-#import "AFURLCache.h"
 
 @implementation AppDelegate
 @synthesize window = _window;
 @synthesize navigationController = _navigationController;
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-    AFURLCache *URLCache = [[[AFURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:[AFURLCache defaultCachePath]] autorelease];
+    NSURLCache *URLCache = [[[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:nil] autorelease];
 	[NSURLCache setSharedURLCache:URLCache];
     
     UITableViewController *viewController = [[[NearbySpotsViewController alloc] init] autorelease];

+ 1 - 0
Example/Classes/AFGowallaAPIClient.h

@@ -27,4 +27,5 @@ extern NSString * const kAFGowallaClientID;
 extern NSString * const kAFGowallaBaseURLString;
 
 @interface AFGowallaAPIClient : AFRestClient
++ (id)sharedClient;
 @end

+ 12 - 0
Example/Classes/AFGowallaAPIClient.m

@@ -22,6 +22,8 @@
 
 #import "AFGowallaAPIClient.h"
 
+static AFGowallaAPIClient *_sharedClient = nil;
+
 // Replace this with your own API Key, available at http://api.gowalla.com/api/keys/
 NSString * const kAFGowallaClientID = @"e7ccb7d3d2414eb2af4663fc91eb2793";
 
@@ -29,6 +31,16 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/";
 
 @implementation AFGowallaAPIClient
 
++ (id)sharedClient {
+    if (_sharedClient == nil) {
+        @synchronized(self) {
+            _sharedClient = [[self alloc] init];
+        }
+    }
+    
+    return _sharedClient;
+}
+
 - (id)init {
     self = [super init];
     if (!self) {

+ 1 - 2
Example/Classes/AFImageRequest.m

@@ -22,7 +22,6 @@
 
 #import "AFImageRequest.h"
 #import "AFImageRequestOperation.h"
-#import "AFURLCache.h"
 
 static NSOperationQueue *_operationQueue = nil;
 static NSMutableSet *_cachedRequests = nil;
@@ -57,7 +56,7 @@ static NSMutableSet *_cachedRequests = nil;
 	AFImageRequestOperationCallback *callback = [AFImageRequestOperationCallback callbackWithSuccess:block imageSize:imageSize options:options];
 	AFImageRequestOperation *operation = [[[AFImageRequestOperation alloc] initWithRequest:request callback:callback] autorelease];
 	
-	NSCachedURLResponse *cachedResponse = [[[[AFURLCache sharedURLCache] cachedResponseForRequest:request] retain] autorelease];
+	NSCachedURLResponse *cachedResponse = [[[[NSURLCache sharedURLCache] cachedResponseForRequest:request] retain] autorelease];
 	if (cachedResponse) {
 		if (block) {
 			block([UIImage imageWithData:[cachedResponse data]]);

Some files were not shown because too many files changed in this diff