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

Conver to Objective-C ARC #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions SYCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,18 @@ @implementation SYCache {
#pragma mark - NSObject

- (id)init {
NSLog(@"[SYCache] You must initalize SYCache using `initWithName:`.");
[self autorelease];
NSAssert(1==0, @"[SYCache] You must initalize SYCache using `initWithName:`.");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop create object with init method at compile time.

return nil;
}


- (void)dealloc {
[_cache removeAllObjects];
[_cache release];
_cache = nil;

dispatch_release(_queue);
_queue = nil;

[_name release];
_name = nil;

[_fileManager release];
_fileManager = nil;

[_cacheDirectory release];
_cacheDirectory = nil;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dispatch queue is an objective object, so it doesn't need to release in ARC.


[super dealloc];
}


Expand Down Expand Up @@ -77,7 +65,7 @@ - (id)initWithName:(NSString *)name {

_fileManager = [[NSFileManager alloc] init];
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
_cacheDirectory = [[cachesDirectory stringByAppendingFormat:@"/com.syntheticcorp.sycache/%@", name] retain];
_cacheDirectory = [cachesDirectory stringByAppendingFormat:@"/com.syntheticcorp.sycache/%@", name];

if (![_fileManager fileExistsAtPath:_cacheDirectory]) {
[_fileManager createDirectoryAtPath:_cacheDirectory withIntermediateDirectories:YES attributes:nil error:nil];
Expand Down Expand Up @@ -111,15 +99,15 @@ - (id)objectForKey:(NSString *)key {
}


- (void)objectForKey:(NSString *)key usingBlock:(void (^)(id object))block {
- (void)objectForKey:(NSString *)key usingBlock:(void (^)(id __autoreleasing object))block {
dispatch_sync(_queue, ^{
id object = [[_cache objectForKey:key] retain];
id object = [_cache objectForKey:key];
if (!object) {
object = [[NSKeyedUnarchiver unarchiveObjectWithFile:[self _pathForKey:key]] retain];
object = [NSKeyedUnarchiver unarchiveObjectWithFile:[self _pathForKey:key]];
[_cache setObject:object forKey:key];
}

block([object autorelease]);
block(object);
});
}

Expand Down Expand Up @@ -232,17 +220,17 @@ - (UIImage *)imageForKey:(NSString *)key {
}


- (void)imageForKey:(NSString *)key usingBlock:(void (^)(UIImage *image))block {
- (void)imageForKey:(NSString *)key usingBlock:(void (^)(UIImage __autoreleasing *image))block {
key = [[self class] _keyForImageKey:key];

dispatch_sync(_queue, ^{
UIImage *image = [[_cache objectForKey:key] retain];
UIImage *image = [_cache objectForKey:key];
if (!image) {
image = [[UIImage alloc] initWithContentsOfFile:[self _pathForKey:key]];
[_cache setObject:image forKey:key];
}

block([image autorelease]);
block(image);
});
}

Expand Down