From c210580de2ae9b4a643de9c110d561356a94b54a Mon Sep 17 00:00:00 2001 From: Alexey Belkevich Date: Sat, 15 Feb 2014 20:06:59 +0200 Subject: [PATCH] Improved callbacks running --- .../Categories/APAsyncDictionary+TrimCount.m | 4 +- Classes/Private/Manager/APTaskManager.m | 44 ++++++------------ Classes/Private/Model/APTaskModel.h | 2 - Classes/Private/Model/APTaskModel.m | 13 ------ Classes/Private/Other/APBlockQueue.h | 15 ------ Classes/Private/Other/APBlockQueue.m | 46 ------------------- Classes/Private/Storages/APMemoryStorage.h | 2 +- Classes/Private/Storages/APMemoryStorage.m | 7 +-- Classes/Public/APSmartStorage.m | 2 +- .../project.pbxproj | 26 ++++------- 10 files changed, 31 insertions(+), 130 deletions(-) delete mode 100644 Classes/Private/Other/APBlockQueue.h delete mode 100644 Classes/Private/Other/APBlockQueue.m diff --git a/Classes/Private/Categories/APAsyncDictionary+TrimCount.m b/Classes/Private/Categories/APAsyncDictionary+TrimCount.m index 98a98db..6cd5f42 100644 --- a/Classes/Private/Categories/APAsyncDictionary+TrimCount.m +++ b/Classes/Private/Categories/APAsyncDictionary+TrimCount.m @@ -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) { diff --git a/Classes/Private/Manager/APTaskManager.m b/Classes/Private/Manager/APTaskManager.m index 9680f8c..fa14a99 100644 --- a/Classes/Private/Manager/APTaskManager.m +++ b/Classes/Private/Manager/APTaskManager.m @@ -6,14 +6,12 @@ // Copyright (c) 2014 alterplay. All rights reserved. // +#import #import "APTaskManager.h" -#import "APBlockQueue.h" -#import "NSThread+Block.h" @interface APTaskManager () { - APBlockQueue *queue; - NSMutableDictionary *tasksDictionary; + APAsyncDictionary *tasksDictionary; } @end @@ -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; } @@ -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 diff --git a/Classes/Private/Model/APTaskModel.h b/Classes/Private/Model/APTaskModel.h index 7a429f9..a110bad 100644 --- a/Classes/Private/Model/APTaskModel.h +++ b/Classes/Private/Model/APTaskModel.h @@ -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; diff --git a/Classes/Private/Model/APTaskModel.m b/Classes/Private/Model/APTaskModel.m index c647a67..a1d691f 100644 --- a/Classes/Private/Model/APTaskModel.m +++ b/Classes/Private/Model/APTaskModel.m @@ -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 @@ -48,7 +36,6 @@ - (void)updateCallbackBlockWithThread:(NSThread *)thread block:(APTaskCallbackBl previousBlock(object, error); threadBlock(object, error); }; - _isShouldRunTask = NO; } } } diff --git a/Classes/Private/Other/APBlockQueue.h b/Classes/Private/Other/APBlockQueue.h deleted file mode 100644 index f718e1d..0000000 --- a/Classes/Private/Other/APBlockQueue.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// APBlockQueue.h -// APSmartStorage -// -// Created by Alexey Belkevich on 1/23/14. -// Copyright (c) 2014 alterplay. All rights reserved. -// - -#import - -@interface APBlockQueue : NSObject - -- (void)enqueueBlock:(void (^)())block; - -@end diff --git a/Classes/Private/Other/APBlockQueue.m b/Classes/Private/Other/APBlockQueue.m deleted file mode 100644 index 15e6e20..0000000 --- a/Classes/Private/Other/APBlockQueue.m +++ /dev/null @@ -1,46 +0,0 @@ -// -// APBlockQueue.m -// APSmartStorage -// -// Created by Alexey Belkevich on 1/23/14. -// Copyright (c) 2014 alterplay. All rights reserved. -// - -#import "APBlockQueue.h" - -@interface APBlockQueue () -{ - dispatch_queue_t queue; -} -@end - -@implementation APBlockQueue - -#pragma mark - life cycle - -- (id)init -{ - self = [super init]; - if (self) - { - NSString *name = [NSString stringWithFormat:@"com.alterplay.blockqueue.%ld", - (unsigned long)self.hash]; - queue = dispatch_queue_create([name cStringUsingEncoding:NSASCIIStringEncoding], NULL); - } - return self; -} - -#pragma mark - public - -- (void)enqueueBlock:(void (^)())block -{ - if (block) - { - dispatch_async(queue, ^ - { - block(); - }); - } -} - -@end diff --git a/Classes/Private/Storages/APMemoryStorage.h b/Classes/Private/Storages/APMemoryStorage.h index 065b6e2..2baba9e 100644 --- a/Classes/Private/Storages/APMemoryStorage.h +++ b/Classes/Private/Storages/APMemoryStorage.h @@ -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; diff --git a/Classes/Private/Storages/APMemoryStorage.m b/Classes/Private/Storages/APMemoryStorage.m index 3505d01..098740b 100644 --- a/Classes/Private/Storages/APMemoryStorage.m +++ b/Classes/Private/Storages/APMemoryStorage.m @@ -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 key, id object) - { - callback ? callback(object) : nil; - }]; + return [dictionary objectForKeySynchronously:url.absoluteString]; } - (void)setObject:(id)object forURL:(NSURL *)url diff --git a/Classes/Public/APSmartStorage.m b/Classes/Public/APSmartStorage.m index 3b36da0..c24c9a8 100644 --- a/Classes/Public/APSmartStorage.m +++ b/Classes/Public/APSmartStorage.m @@ -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 diff --git a/Spec/APSmartStorageSpec.xcodeproj/project.pbxproj b/Spec/APSmartStorageSpec.xcodeproj/project.pbxproj index 5d5b887..d77acf7 100644 --- a/Spec/APSmartStorageSpec.xcodeproj/project.pbxproj +++ b/Spec/APSmartStorageSpec.xcodeproj/project.pbxproj @@ -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 */ @@ -63,8 +62,6 @@ FAD37B81189110F000825EB6 /* NSFileManager+Storage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileManager+Storage.m"; sourceTree = ""; }; FAD37B891891116D00825EB6 /* APTaskManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = APTaskManager.h; path = Manager/APTaskManager.h; sourceTree = ""; }; FAD37B8A1891116D00825EB6 /* APTaskManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = APTaskManager.m; path = Manager/APTaskManager.m; sourceTree = ""; }; - FAD37B9A1891411200825EB6 /* APBlockQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APBlockQueue.h; sourceTree = ""; }; - FAD37B9B1891411200825EB6 /* APBlockQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APBlockQueue.m; sourceTree = ""; }; FAE4DAD41887BA5B003541A9 /* APSmartStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APSmartStorage.h; sourceTree = ""; }; FAE4DAD51887BA5B003541A9 /* APSmartStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APSmartStorage.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -157,13 +154,21 @@ name = Spec; sourceTree = ""; }; + FAA0FC8818AFDFEA00BAAC5D /* Network */ = { + isa = PBXGroup; + children = ( + FAD37B78189110F000825EB6 /* APNetworkStorage.h */, + FAD37B79189110F000825EB6 /* APNetworkStorage.m */, + ); + name = Network; + sourceTree = ""; + }; FAD37B73189110F000825EB6 /* Storages */ = { isa = PBXGroup; children = ( FAD37B9D18915AFB00825EB6 /* Memory */, FAD37B9E189168F200825EB6 /* File */, - FAD37B78189110F000825EB6 /* APNetworkStorage.h */, - FAD37B79189110F000825EB6 /* APNetworkStorage.m */, + FAA0FC8818AFDFEA00BAAC5D /* Network */, ); path = Storages; sourceTree = ""; @@ -197,15 +202,6 @@ name = Manager; sourceTree = ""; }; - FAD37B991891411200825EB6 /* Other */ = { - isa = PBXGroup; - children = ( - FAD37B9A1891411200825EB6 /* APBlockQueue.h */, - FAD37B9B1891411200825EB6 /* APBlockQueue.m */, - ); - path = Other; - sourceTree = ""; - }; FAD37B9D18915AFB00825EB6 /* Memory */ = { isa = PBXGroup; children = ( @@ -241,7 +237,6 @@ FAD37B8C1891117400825EB6 /* Manager */, FAD37B73189110F000825EB6 /* Storages */, FAD37B7D189110F000825EB6 /* Categories */, - FAD37B991891411200825EB6 /* Other */, ); path = Private; sourceTree = ""; @@ -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 */,