Skip to content

Commit

Permalink
Improved callbacks running
Browse files Browse the repository at this point in the history
  • Loading branch information
belkevich committed Feb 15, 2014
1 parent 194caf2 commit c210580
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 130 deletions.
4 changes: 2 additions & 2 deletions Classes/Private/Categories/APAsyncDictionary+TrimCount.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#import "APAsyncDictionary+TrimCount.h"

@interface APAsyncDictionary ()
- (void)runDictionaryOperationBlock:(void(^)(NSMutableDictionary *dictionary))operationBlock;
- (void)runDictionaryAsynchronousBlock:(void(^)(NSMutableDictionary *dictionary))operationBlock;
@end

@implementation APAsyncDictionary (TrimCount)

- (void)trimObjectsToCount:(NSUInteger)maxCount
{
[self runDictionaryOperationBlock:^(NSMutableDictionary *dictionary)
[self runDictionaryAsynchronousBlock:^(NSMutableDictionary *dictionary)
{
while (dictionary.count > maxCount)
{
Expand Down
44 changes: 15 additions & 29 deletions Classes/Private/Manager/APTaskManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
// Copyright (c) 2014 alterplay. All rights reserved.
//

#import <APAsyncDictionary/APAsyncDictionary.h>
#import "APTaskManager.h"
#import "APBlockQueue.h"
#import "NSThread+Block.h"

@interface APTaskManager ()
{
APBlockQueue *queue;
NSMutableDictionary *tasksDictionary;
APAsyncDictionary *tasksDictionary;
}
@end

Expand All @@ -26,8 +24,7 @@ - (id)init
self = [super init];
if (self)
{
tasksDictionary = [[NSMutableDictionary alloc] init];
queue = [[APBlockQueue alloc] init];
tasksDictionary = [[APAsyncDictionary alloc] init];
}
return self;
}
Expand All @@ -40,36 +37,25 @@ - (void)taskWithURL:(NSURL *)url block:(APTaskCallbackBlock)block
NSString *key = url.absoluteString;
if (key)
{
__weak NSMutableDictionary *weakDictionary = tasksDictionary;
__weak NSThread *weakThread = NSThread.currentThread;
[queue enqueueBlock:^
BOOL isShouldRunTask = NO;
APTaskModel *task = [tasksDictionary objectForKeySynchronously:key];
if (!task)
{
APTaskModel *task = [weakDictionary objectForKey:key];
if (!task)
{
task = [[APTaskModel alloc] init];
[weakDictionary setObject:task forKey:key];
}
[task updateCallbackBlockWithThread:weakThread block:block];
BOOL run = task.isShouldRunTask;
[NSThread performOnThread:weakThread block:^
{
callback(run);
}];
}];
task = [[APTaskModel alloc] init];
[tasksDictionary setObject:task forKey:key];
isShouldRunTask = YES;
}
[task updateCallbackBlockWithThread:NSThread.currentThread block:block];
callback(isShouldRunTask);
}
}

- (void)finishTaskWithURL:(NSURL *)url object:(id)object error:(NSError *)error
{
NSString *key = url.absoluteString;
__weak NSMutableDictionary *weakDictionary = tasksDictionary;
[queue enqueueBlock:^
{
APTaskModel *task = [weakDictionary objectForKey:key];
[weakDictionary removeObjectForKey:key];
[task performCallbackBlockWithObject:object error:error];
}];
APTaskModel *task = [tasksDictionary objectForKeySynchronously:key];
[tasksDictionary removeObjectForKey:key];
[task performCallbackBlockWithObject:object error:error];
}

@end
2 changes: 0 additions & 2 deletions Classes/Private/Model/APTaskModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ typedef void (^APTaskCallbackBlock)(id object, NSError *error);

@interface APTaskModel : NSObject

@property (nonatomic, readonly) BOOL isShouldRunTask;

- (void)updateCallbackBlockWithThread:(NSThread *)thread block:(APTaskCallbackBlock)block;
- (void)performCallbackBlockWithObject:(id)object error:(NSError *)error;

Expand Down
13 changes: 0 additions & 13 deletions Classes/Private/Model/APTaskModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ @interface APTaskModel ()

@implementation APTaskModel

#pragma mark - life cycle

- (id)init
{
self = [super init];
if (self)
{
_isShouldRunTask = YES;
}
return self;
}

#pragma mark - public

- (void)updateCallbackBlockWithThread:(NSThread *)thread block:(APTaskCallbackBlock)block
Expand All @@ -48,7 +36,6 @@ - (void)updateCallbackBlockWithThread:(NSThread *)thread block:(APTaskCallbackBl
previousBlock(object, error);
threadBlock(object, error);
};
_isShouldRunTask = NO;
}
}
}
Expand Down
15 changes: 0 additions & 15 deletions Classes/Private/Other/APBlockQueue.h

This file was deleted.

46 changes: 0 additions & 46 deletions Classes/Private/Other/APBlockQueue.m

This file was deleted.

2 changes: 1 addition & 1 deletion Classes/Private/Storages/APMemoryStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@property (nonatomic, assign) NSUInteger maxCount;

- (id)initWithMaxObjectCount:(NSUInteger)count;
- (void)objectWithURL:(NSURL *)url callback:(void(^)(id object))callback;
- (id)objectWithURL:(NSURL *)url;
- (void)setObject:(id)object forURL:(NSURL *)url;
- (void)removeObjectForURL:(NSURL *)url;
- (void)removeAllObjects;
Expand Down
7 changes: 2 additions & 5 deletions Classes/Private/Storages/APMemoryStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ - (id)initWithMaxObjectCount:(NSUInteger)count

#pragma mark - public

- (void)objectWithURL:(NSURL *)url callback:(void (^)(id object))callback
- (id)objectWithURL:(NSURL *)url
{
[dictionary objectForKey:url.absoluteString callback:^(id <NSCopying> key, id object)
{
callback ? callback(object) : nil;
}];
return [dictionary objectForKeySynchronously:url.absoluteString];
}

- (void)setObject:(id)object forURL:(NSURL *)url
Expand Down
2 changes: 1 addition & 1 deletion Classes/Public/APSmartStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ - (void)objectFromStorageWithURL:(NSURL *)url callback:(void (^)(id object, NSEr

- (void)objectFromMemoryWithURL:(NSURL *)objectURL callback:(void (^)(id object))callback
{
[self.memoryStorage objectWithURL:objectURL callback:callback];
callback([self.memoryStorage objectWithURL:objectURL]);
}

- (void)objectFromFileWithURL:(NSURL *)url callback:(void (^)(id object, NSError *error))callback
Expand Down
26 changes: 10 additions & 16 deletions Spec/APSmartStorageSpec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
FAD37B87189110F000825EB6 /* APAsyncDictionary+TrimCount.m in Sources */ = {isa = PBXBuildFile; fileRef = FAD37B7F189110F000825EB6 /* APAsyncDictionary+TrimCount.m */; };
FAD37B88189110F000825EB6 /* NSFileManager+Storage.m in Sources */ = {isa = PBXBuildFile; fileRef = FAD37B81189110F000825EB6 /* NSFileManager+Storage.m */; };
FAD37B8B1891116D00825EB6 /* APTaskManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FAD37B8A1891116D00825EB6 /* APTaskManager.m */; };
FAD37B9C1891411200825EB6 /* APBlockQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = FAD37B9B1891411200825EB6 /* APBlockQueue.m */; };
FAE4DAD61887BA5B003541A9 /* APSmartStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = FAE4DAD51887BA5B003541A9 /* APSmartStorage.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -63,8 +62,6 @@
FAD37B81189110F000825EB6 /* NSFileManager+Storage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileManager+Storage.m"; sourceTree = "<group>"; };
FAD37B891891116D00825EB6 /* APTaskManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = APTaskManager.h; path = Manager/APTaskManager.h; sourceTree = "<group>"; };
FAD37B8A1891116D00825EB6 /* APTaskManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = APTaskManager.m; path = Manager/APTaskManager.m; sourceTree = "<group>"; };
FAD37B9A1891411200825EB6 /* APBlockQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APBlockQueue.h; sourceTree = "<group>"; };
FAD37B9B1891411200825EB6 /* APBlockQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APBlockQueue.m; sourceTree = "<group>"; };
FAE4DAD41887BA5B003541A9 /* APSmartStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APSmartStorage.h; sourceTree = "<group>"; };
FAE4DAD51887BA5B003541A9 /* APSmartStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APSmartStorage.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -157,13 +154,21 @@
name = Spec;
sourceTree = "<group>";
};
FAA0FC8818AFDFEA00BAAC5D /* Network */ = {
isa = PBXGroup;
children = (
FAD37B78189110F000825EB6 /* APNetworkStorage.h */,
FAD37B79189110F000825EB6 /* APNetworkStorage.m */,
);
name = Network;
sourceTree = "<group>";
};
FAD37B73189110F000825EB6 /* Storages */ = {
isa = PBXGroup;
children = (
FAD37B9D18915AFB00825EB6 /* Memory */,
FAD37B9E189168F200825EB6 /* File */,
FAD37B78189110F000825EB6 /* APNetworkStorage.h */,
FAD37B79189110F000825EB6 /* APNetworkStorage.m */,
FAA0FC8818AFDFEA00BAAC5D /* Network */,
);
path = Storages;
sourceTree = "<group>";
Expand Down Expand Up @@ -197,15 +202,6 @@
name = Manager;
sourceTree = "<group>";
};
FAD37B991891411200825EB6 /* Other */ = {
isa = PBXGroup;
children = (
FAD37B9A1891411200825EB6 /* APBlockQueue.h */,
FAD37B9B1891411200825EB6 /* APBlockQueue.m */,
);
path = Other;
sourceTree = "<group>";
};
FAD37B9D18915AFB00825EB6 /* Memory */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -241,7 +237,6 @@
FAD37B8C1891117400825EB6 /* Manager */,
FAD37B73189110F000825EB6 /* Storages */,
FAD37B7D189110F000825EB6 /* Categories */,
FAD37B991891411200825EB6 /* Other */,
);
path = Private;
sourceTree = "<group>";
Expand Down Expand Up @@ -362,7 +357,6 @@
FAD37B86189110F000825EB6 /* APTaskModel.m in Sources */,
FAD37B85189110F000825EB6 /* APNetworkStorage.m in Sources */,
FAD37B84189110F000825EB6 /* APMemoryStorage.m in Sources */,
FAD37B9C1891411200825EB6 /* APBlockQueue.m in Sources */,
FACB9664189292C000739080 /* APSmartStorageTaskSpec.mm in Sources */,
FA68F29A188FC56E007CCE3E /* APSmartStorageBaseSpec.mm in Sources */,
FAD37B83189110F000825EB6 /* APFileStorage.m in Sources */,
Expand Down

0 comments on commit c210580

Please sign in to comment.