diff --git a/AFCache.xcodeproj/project.pbxproj b/AFCache.xcodeproj/project.pbxproj index abe5551..d158851 100644 --- a/AFCache.xcodeproj/project.pbxproj +++ b/AFCache.xcodeproj/project.pbxproj @@ -77,7 +77,6 @@ 05FA238B1357544B00050BCB /* AFCacheableItemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAE8132A291B0087CEA1 /* AFCacheableItemInfo.h */; }; 05FA238C1357544C00050BCB /* AFPackageInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEA132A291B0087CEA1 /* AFPackageInfo.h */; }; 05FA238D1357544D00050BCB /* AFURLCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEC132A291B0087CEA1 /* AFURLCache.h */; }; - 05FA238E1357544F00050BCB /* Constants.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEE132A291B0087CEA1 /* Constants.h */; }; 05FA238F1357545000050BCB /* DateParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 05C9BAEF132A291B0087CEA1 /* DateParser.h */; }; 05FA23901357548D00050BCB /* libAFCacheOSXStatic.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 05FA235F1357539B00050BCB /* libAFCacheOSXStatic.a */; }; C73C71CC19816F13008EDA23 /* AFRequestConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = C73C71CA19816F13008EDA23 /* AFRequestConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -88,6 +87,9 @@ C7503D47198640AA0032E451 /* AFDownloadOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = C7503D44198640AA0032E451 /* AFDownloadOperation.h */; }; C7503D48198640AA0032E451 /* AFDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = C7503D45198640AA0032E451 /* AFDownloadOperation.m */; }; C7503D49198640AA0032E451 /* AFDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = C7503D45198640AA0032E451 /* AFDownloadOperation.m */; }; + C799310A1987B974001A7724 /* AFCacheManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C79931081987B974001A7724 /* AFCacheManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; + C799310B1987B974001A7724 /* AFCacheManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C79931091987B974001A7724 /* AFCacheManager.m */; }; + C799310C1987C0BF001A7724 /* AFCacheManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C79931081987B974001A7724 /* AFCacheManager.h */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -174,7 +176,6 @@ 05C9BAEB132A291B0087CEA1 /* AFPackageInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFPackageInfo.m; path = src/shared/AFPackageInfo.m; sourceTree = ""; }; 05C9BAEC132A291B0087CEA1 /* AFURLCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFURLCache.h; path = src/shared/AFURLCache.h; sourceTree = ""; }; 05C9BAED132A291B0087CEA1 /* AFURLCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFURLCache.m; path = src/shared/AFURLCache.m; sourceTree = ""; }; - 05C9BAEE132A291B0087CEA1 /* Constants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Constants.h; path = src/shared/Constants.h; sourceTree = ""; }; 05C9BAEF132A291B0087CEA1 /* DateParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DateParser.h; path = src/shared/DateParser.h; sourceTree = ""; }; 05C9BAF0132A291B0087CEA1 /* DateParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DateParser.m; path = src/shared/DateParser.m; sourceTree = ""; }; 05C9BB08132A29370087CEA1 /* AFRegexString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFRegexString.h; sourceTree = ""; }; @@ -214,6 +215,8 @@ C73C71CB19816F13008EDA23 /* AFRequestConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFRequestConfiguration.m; path = src/shared/AFRequestConfiguration.m; sourceTree = ""; }; C7503D44198640AA0032E451 /* AFDownloadOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFDownloadOperation.h; path = src/shared/AFDownloadOperation.h; sourceTree = ""; }; C7503D45198640AA0032E451 /* AFDownloadOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFDownloadOperation.m; path = src/shared/AFDownloadOperation.m; sourceTree = ""; }; + C79931081987B974001A7724 /* AFCacheManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFCacheManager.h; path = src/shared/AFCacheManager.h; sourceTree = ""; }; + C79931091987B974001A7724 /* AFCacheManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFCacheManager.m; path = src/shared/AFCacheManager.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -368,7 +371,6 @@ 05238D14151B6E470015D70E /* core */ = { isa = PBXGroup; children = ( - 05C9BAEE132A291B0087CEA1 /* Constants.h */, 05C9BADD132A291B0087CEA1 /* AFCache.h */, 05C9BADE132A291B0087CEA1 /* AFCache.m */, 05C9BAE3132A291B0087CEA1 /* AFCache+PrivateAPI.h */, @@ -380,6 +382,8 @@ C73C71CB19816F13008EDA23 /* AFRequestConfiguration.m */, C7503D44198640AA0032E451 /* AFDownloadOperation.h */, C7503D45198640AA0032E451 /* AFDownloadOperation.m */, + C79931081987B974001A7724 /* AFCacheManager.h */, + C79931091987B974001A7724 /* AFCacheManager.m */, ); name = core; sourceTree = ""; @@ -536,6 +540,7 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + C799310A1987B974001A7724 /* AFCacheManager.h in Headers */, 05C9BB6F132A30E60087CEA1 /* AFCacheLib.h in Headers */, 046BEFAB152D180A00FE16B8 /* AFCache.h in Headers */, 046BEFAC152D180A00FE16B8 /* AFCacheableItem.h in Headers */, @@ -559,8 +564,8 @@ 05FA23841357543A00050BCB /* AFCache_Logging.h in Headers */, 05FA23891357544400050BCB /* AFCacheableItem.h in Headers */, 05FA238B1357544B00050BCB /* AFCacheableItemInfo.h in Headers */, + C799310C1987C0BF001A7724 /* AFCacheManager.h in Headers */, 05FA238C1357544C00050BCB /* AFPackageInfo.h in Headers */, - 05FA238E1357544F00050BCB /* Constants.h in Headers */, 05FA238F1357545000050BCB /* DateParser.h in Headers */, 05FA238D1357544D00050BCB /* AFURLCache.h in Headers */, C7503D47198640AA0032E451 /* AFDownloadOperation.h in Headers */, @@ -796,6 +801,7 @@ buildActionMask = 2147483647; files = ( 05C9BAF3132A291B0087CEA1 /* AFCache.m in Sources */, + C799310B1987B974001A7724 /* AFCacheManager.m in Sources */, 05C9BAF5132A291B0087CEA1 /* AFCache+Mimetypes.m in Sources */, 05C9BAFA132A291B0087CEA1 /* AFCacheableItem.m in Sources */, 05C9BAFC132A291B0087CEA1 /* AFCacheableItem+Packaging.m in Sources */, @@ -901,6 +907,7 @@ isa = XCBuildConfiguration; buildSettings = { ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; + CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; @@ -925,6 +932,7 @@ isa = XCBuildConfiguration; buildSettings = { ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; + CLANG_ENABLE_OBJC_ARC = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; diff --git a/src/shared/AFCache.h b/src/shared/AFCache.h index 3ce7428..0da594e 100755 --- a/src/shared/AFCache.h +++ b/src/shared/AFCache.h @@ -184,8 +184,6 @@ typedef struct NetworkTimeoutIntervals { */ @property (nonatomic, assign) BOOL disableSSLCertificateValidation; -+ (AFCache*)cacheForContext:(NSString*)context; - - (NSString *)filenameForURL: (NSURL *) url; - (NSString *)filenameForURLString: (NSString *) URLString; - (NSString *)filePath: (NSString *) filename; @@ -193,7 +191,8 @@ typedef struct NetworkTimeoutIntervals { - (NSString *)fullPathForCacheableItem:(AFCacheableItem*)item; -+ (AFCache *)sharedInstance; ++ (AFCache *)sharedInstance __attribute__((deprecated("use defaultCache instead"))); ++ (AFCache *)defaultCache; - (AFCacheableItem *)cachedObjectForURL: (NSURL *) url diff --git a/src/shared/AFCache.m b/src/shared/AFCache.m index 1801265..88cbc2a 100755 --- a/src/shared/AFCache.m +++ b/src/shared/AFCache.m @@ -29,6 +29,7 @@ #include #import "AFRegexString.h" #import "AFCache_Logging.h" +#import "AFCacheManager.h" #if USE_ASSERTS #define ASSERT_NO_CONNECTION_WHEN_OFFLINE_FOR_URL(url) NSAssert( [(url) isFileURL] || [self isOffline] == NO, @"No connection should be opened if we're in offline mode - this seems like a bug") @@ -47,7 +48,6 @@ @interface AFCache() -@property (nonatomic, copy) NSString *context; @property (nonatomic, strong) NSMutableArray *downloadQueue; @property (nonatomic, strong) NSTimer *archiveTimer; @property (nonatomic, assign) BOOL wantsToArchive; @@ -56,34 +56,25 @@ @interface AFCache() - (void)serializeState:(NSDictionary*)infoStore; - (void)cancelAllClientItems; -- (id)initWithContext:(NSString*)context; @end @implementation AFCache -static AFCache *sharedAFCacheInstance = nil; -static NSMutableDictionary* AFCache_contextCache = nil; - #pragma mark singleton methods + (AFCache *)sharedInstance { - @synchronized(self) { - if (sharedAFCacheInstance == nil) { - sharedAFCacheInstance = [[self alloc] initWithContext:nil]; - sharedAFCacheInstance.diskCacheDisplacementTresholdSize = kDefaultDiskCacheDisplacementTresholdSize; - } - } - return sharedAFCacheInstance; + // left for backward compability + return [AFCacheManager defaultCache]; +} + ++ (AFCache *)defaultCache { + return [AFCacheManager defaultCache]; } #pragma mark init methods -- (id)initWithContext:(NSString*)context { - if (!context && sharedAFCacheInstance != nil) - { - return [AFCache sharedInstance]; - } - +- (id)init +{ self = [super init]; if (self) { @@ -99,17 +90,11 @@ - (id)initWithContext:(NSString*)context { name:UIApplicationWillTerminateNotification object:nil]; #endif - if (!AFCache_contextCache) { - AFCache_contextCache = [[NSMutableDictionary alloc] init]; - } - - if (context) { - [AFCache_contextCache setObject:[NSValue valueWithPointer:(__bridge const void *)(self)] forKey:context]; - } - - _context = [context copy]; + [self reinitialize]; [self initMimeTypes]; + + self.diskCacheDisplacementTresholdSize = kDefaultDiskCacheDisplacementTresholdSize; } return self; } @@ -166,12 +151,6 @@ - (void)initialize { - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - - if (_context) - { - [AFCache_contextCache removeObjectForKey:_context]; - } - } - (NSUInteger)requestsPending { @@ -179,7 +158,7 @@ - (NSUInteger)requestsPending { } - (void)setDataPath:(NSString*)newDataPath { - if (self.context && self.dataPath) + if (self.dataPath) { NSLog(@"Error: Can't change data path on instanced AFCache"); NSAssert(NO, @"Can't change data path on instanced AFCache"); @@ -194,28 +173,6 @@ - (void)setDataPath:(NSString*)newDataPath { self.maxItemFileSize = fileSize; } -// TODO: If we really need "named" caches ("context" is the wrong word), then realize this concept as a category, but not here -+ (AFCache*)cacheForContext:(NSString *)context -{ - if (!AFCache_contextCache) - { - AFCache_contextCache = [[NSMutableDictionary alloc] init]; - } - - if (!context) - { - return [self sharedInstance]; - } - - AFCache* cache = [[AFCache_contextCache objectForKey:context] pointerValue]; - if (!cache) - { - cache = [[[self class] alloc] initWithContext:context]; - } - - return cache; -} - // The method reinitialize really initializes the cache. // This is usefull for testing, when you want to, uh, reinitialize @@ -249,7 +206,7 @@ -(void)addRedirectFromURL:(NSURL*)originalURL toURL:(NSURL*)redirectURL [self.urlRedirects setObject:[redirectURL absoluteString] forKey:[originalURL absoluteString]]; } --(void)addRedirectFromURLString:(NSString*)originalURLString toURLString:(NSString*)redirectURLString +-(void)addRedirectFromURLString:(NSString*)originalURLString toURL:(NSString*)redirectURLString { [self.urlRedirects setObject:redirectURLString forKey:originalURLString]; } diff --git a/src/shared/AFCacheManager.h b/src/shared/AFCacheManager.h new file mode 100644 index 0000000..7404a06 --- /dev/null +++ b/src/shared/AFCacheManager.h @@ -0,0 +1,25 @@ +// +// AFCacheManager.h +// AFCache +// +// Created by Sebastian Grimme on 29.07.14. +// Copyright (c) 2014 Artifacts - Fine Software Development. All rights reserved. +// + +#import "AFCache.h" + +extern NSString *kAFCacheDefaultName; + +@interface AFCacheManager : NSObject + +/** + * @return default cache instance + */ ++ (AFCache*)defaultCache; + +/** + * @return named cache instance + */ ++ (AFCache*)cacheNamed:(NSString*)name; + +@end diff --git a/src/shared/AFCacheManager.m b/src/shared/AFCacheManager.m new file mode 100644 index 0000000..26118d1 --- /dev/null +++ b/src/shared/AFCacheManager.m @@ -0,0 +1,73 @@ +// +// AFCacheManager.m +// AFCache +// +// Created by Sebastian Grimme on 29.07.14. +// Copyright (c) 2014 Artifacts - Fine Software Development. All rights reserved. +// + +#import "AFCacheManager.h" + +NSString *kAFCacheDefaultName = @"AFCacheDefaultName"; + +static AFCacheManager *sharedAFCacheManagerInstance = nil; + +@interface AFCacheManager () +@property (nonatomic, strong) NSMutableDictionary* instanceDictionary; +@end + +@implementation AFCacheManager + +#pragma mark singleton methods + ++ (AFCacheManager*)sharedManager { + @synchronized(self) { + if (sharedAFCacheManagerInstance == nil) { + sharedAFCacheManagerInstance = [[self alloc] init]; + } + } + return sharedAFCacheManagerInstance; +} + +#pragma mark - Lifecycle + +- (instancetype)init +{ + self = [super init]; + if (self) { + // create dictionary which holds all named cache-instances + _instanceDictionary = [[NSMutableDictionary alloc] init]; + } + return self; +} + +#pragma mark - Private API + +- (AFCache*)cacheInstanceNamed:(NSString *)name +{ + @synchronized (self.instanceDictionary) { + AFCache *cacheInstance = [[AFCacheManager sharedManager].instanceDictionary objectForKey:name]; + + if (!cacheInstance) { + cacheInstance = [[AFCache alloc] init]; + + [[AFCacheManager sharedManager].instanceDictionary setObject:cacheInstance forKey:name]; + } + + return cacheInstance; + } +} + +#pragma mark - static factory/get methods + ++ (AFCache*)defaultCache +{ + return [[AFCacheManager sharedManager] cacheInstanceNamed:kAFCacheDefaultName]; +} + ++ (AFCache*)cacheNamed:(NSString*)name +{ + return [[AFCacheManager sharedManager] cacheInstanceNamed:name]; +} + +@end diff --git a/src/shared/AFCacheableItem.m b/src/shared/AFCacheableItem.m index d465e70..03b9cee 100644 --- a/src/shared/AFCacheableItem.m +++ b/src/shared/AFCacheableItem.m @@ -65,6 +65,8 @@ - (AFCacheableItem*)initWithURL:(NSURL*)URL _url = URL; _cacheStatus = kCacheStatusFresh; _validUntil = _info.expireDate; + + // TODO: We cannot assume that this item's cache is the default sharedInstance _cache = [AFCache sharedInstance]; } return self; diff --git a/src/shared/AFHTTPURLProtocol.m b/src/shared/AFHTTPURLProtocol.m index d975f4f..1bd67a8 100644 --- a/src/shared/AFHTTPURLProtocol.m +++ b/src/shared/AFHTTPURLProtocol.m @@ -71,7 +71,8 @@ - (id)initWithRequest:(NSURLRequest *)aRequest - (void)startLoading { - [[AFCache sharedInstance] cachedObjectForRequest:self.request delegate:self]; + // this URL-Protocol works with defaultCache only + [[AFCache defaultCache] cachedObjectForRequest:self.request delegate:self]; } - (void) connectionDidFail: (AFCacheableItem *) cacheableItem { @@ -98,7 +99,8 @@ - (void)connectionHasBeenRedirected: (AFCacheableItem*) cacheableItem { - (void)stopLoading { - [[AFCache sharedInstance] cancelAsynchronousOperationsForURL:[[self request] URL] itemDelegate:self]; + // this URL-Protocol works with defaultCache only + [[AFCache defaultCache] cancelAsynchronousOperationsForURL:[[self request] URL] itemDelegate:self]; } - (NSCachedURLResponse *)cachedResponse { diff --git a/src/shared/AFURLCache.m b/src/shared/AFURLCache.m index 75c3805..fc0323f 100755 --- a/src/shared/AFURLCache.m +++ b/src/shared/AFURLCache.m @@ -29,7 +29,9 @@ @implementation AFURLCache -(NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request { NSURL* url = request.URL; - AFCacheableItem* item = [[AFCache sharedInstance] cacheableItemFromCacheStore:url]; + + // this URL-Cache works with defaultCache only + AFCacheableItem* item = [[AFCache defaultCache] cacheableItemFromCacheStore:url]; if (item && item.cacheStatus == kCacheStatusFresh) { AFMediaTypeParser *parser = [[AFMediaTypeParser alloc] initWithMIMEType:item.info.mimeType]; @@ -64,7 +66,9 @@ - (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NS expireDate = (expiresHeader) ? [DateParser gh_parseHTTP: expiresHeader] : nil; AFCacheableItem *item = [[AFCacheableItem alloc] initWithURL:request.URL lastModified:lastModified expireDate:expireDate contentType:contentTypeHeader]; - [[AFCache sharedInstance] importCacheableItem:item withData:cachedResponse.data]; + + // this URL-Cache works with defaultCache only + [[AFCache defaultCache] importCacheableItem:item withData:cachedResponse.data]; } }