Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to change directory for cache, ability to set key for HNKDiskCache #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Haneke/HNKCache.h
Expand Up @@ -45,6 +45,19 @@
*/
- (id)initWithName:(NSString*)name;

/**
Initializes a cache with the given name.
@param name Name of the cache. Used as the name for the subdirectory of the disk cache.
@param directory Path of an existing directory. Used as directory of the disk cache.
*/
- (id)initWithName:(NSString*)name directory:(NSString *)directory;

/**
Sets the shared cache with the given cache.
@param cache Instance of the cache. Used by the UIKit categories.
*/
+ (void)setSharedCache:(HNKCache *)cache;

/**
Returns the shared cache used by the UIKit categories.
@discussion It is recommended to use the shared cache unless you need separate caches.
Expand Down
38 changes: 29 additions & 9 deletions Haneke/HNKCache.m
Expand Up @@ -68,16 +68,21 @@ @implementation HNKCache {
#pragma mark Initializing the cache

- (id)initWithName:(NSString*)name
{
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
return [self initWithName:name directory:cachesDirectory];
}

- (id)initWithName:(NSString*)name directory:(NSString *)directory
{
self = [super init];
if (self)
{
_memoryCaches = [NSMutableDictionary dictionary];
_formats = [NSMutableDictionary dictionary];

NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
static NSString *cachePathComponent = @"com.hpique.haneke";
NSString *path = [cachesDirectory stringByAppendingPathComponent:cachePathComponent];
static NSString *subdirectoryPathComponent = @"com.hpique.haneke";
NSString *path = [directory stringByAppendingPathComponent:subdirectoryPathComponent];
_rootDirectory = [path stringByAppendingPathComponent:name];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
Expand All @@ -90,16 +95,31 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}

static HNKCache *sharedCache = nil;

+ (HNKCache*)sharedCache
{
static HNKCache *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[HNKCache alloc] initWithName:@"shared"];
});
return instance;
if (sharedCache)
{
return sharedCache;
}
else
{
static HNKCache *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[HNKCache alloc] initWithName:@"shared"];
});
return instance;
}
}

+ (void)setSharedCache:(HNKCache *)cache
{
sharedCache = cache;
}


- (void)registerFormat:(HNKCacheFormat *)format
{
NSString *formatName = format.name;
Expand Down
8 changes: 8 additions & 0 deletions Haneke/HNKDiskFetcher.h
Expand Up @@ -34,9 +34,17 @@ enum
/**
Initializes a fetcher with the given path.
@param path Image path.
@warning Key will be equal to path. Path to the application directory (Documents, Caches, etc) can be different after relaunch application.
*/
- (instancetype)initWithPath:(NSString*)path;

/**
Initializes a fetcher with the given path and key.
@param path Image path.
@param path Image key.
*/
- (instancetype)initWithPath:(NSString*)path andKey:(NSString *)key;

/**
Cancels the current fetch. When a fetch is cancelled it should not call any of the provided blocks.
@discussion This will be typically used by UI logic to cancel fetches during view reuse.
Expand Down
9 changes: 8 additions & 1 deletion Haneke/HNKDiskFetcher.m
Expand Up @@ -22,21 +22,28 @@

@implementation HNKDiskFetcher {
NSString *_path;
NSString *_key;
BOOL _cancelled;
}

- (instancetype)initWithPath:(NSString*)path
{
return [self initWithPath:path andKey:path];
}

- (instancetype)initWithPath:(NSString*)path andKey:(NSString *)key
{
if (self = [super init])
{
_path = path;
_key = key;
}
return self;
}

- (NSString*)key
{
return _path;
return _key;
}

- (void)fetchImageWithSuccess:(void (^)(UIImage *image))successBlock failure:(void (^)(NSError *error))failureBlock;
Expand Down