diff --git a/AFCache.xcodeproj/project.pbxproj b/AFCache.xcodeproj/project.pbxproj index abe5551..85ad24a 100644 --- a/AFCache.xcodeproj/project.pbxproj +++ b/AFCache.xcodeproj/project.pbxproj @@ -88,6 +88,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 */ @@ -214,6 +217,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 */ @@ -380,6 +385,8 @@ C73C71CB19816F13008EDA23 /* AFRequestConfiguration.m */, C7503D44198640AA0032E451 /* AFDownloadOperation.h */, C7503D45198640AA0032E451 /* AFDownloadOperation.m */, + C79931081987B974001A7724 /* AFCacheManager.h */, + C79931091987B974001A7724 /* AFCacheManager.m */, ); name = core; sourceTree = ""; @@ -536,6 +543,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,6 +567,7 @@ 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 */, @@ -796,6 +805,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 +911,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 +936,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..97f45d4 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; diff --git a/src/shared/AFCache.m b/src/shared/AFCache.m index 1801265..a15251d 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,21 @@ @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]; } #pragma mark init methods -- (id)initWithContext:(NSString*)context { - if (!context && sharedAFCacheInstance != nil) - { - return [AFCache sharedInstance]; - } - +- (id)init +{ self = [super init]; if (self) { @@ -99,17 +86,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 +147,6 @@ - (void)initialize { - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - - if (_context) - { - [AFCache_contextCache removeObjectForKey:_context]; - } - } - (NSUInteger)requestsPending { @@ -179,7 +154,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 +169,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 +202,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..45c2fbd --- /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*)cacheForName:(NSString*)name; + +@end diff --git a/src/shared/AFCacheManager.m b/src/shared/AFCacheManager.m new file mode 100644 index 0000000..8d374fd --- /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 cache-instances + _instanceDictionary = [[NSMutableDictionary alloc] init]; + } + return self; +} + +#pragma mark - Private API + +- (AFCache*)cacheInstanceForName:(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] cacheInstanceForName:kAFCacheDefaultName]; +} + ++ (AFCache*)cacheForName:(NSString*)name +{ + return [[AFCacheManager sharedManager] cacheInstanceForName:name]; +} + +@end