Skip to content

Commit

Permalink
configurable completion queue for async calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kolyvan committed Nov 10, 2015
1 parent d412e5c commit eed7681
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
1 change: 1 addition & 0 deletions KxSMBProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ typedef NS_ENUM(NSUInteger, KxSMBConfigEncryptLevel) {

@property (readwrite, nonatomic, weak) id<KxSMBProviderDelegate> delegate;
@property (readwrite, nonatomic, strong, nonnull) KxSMBConfig *config;
@property (readwrite, nonatomic, strong, nullable) dispatch_queue_t completionQueue;

+ (nullable instancetype) sharedSmbProvider;

Expand Down
54 changes: 22 additions & 32 deletions KxSMBProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ - (id) init

_config = [KxSMBConfig new];
_dispatchQueue = dispatch_queue_create("KxSMBProvider", DISPATCH_QUEUE_SERIAL);
_completionQueue = dispatch_get_main_queue();
}
return self;
}
Expand Down Expand Up @@ -1369,6 +1370,16 @@ + (id) renameAtPath:(NSString *)oldPath
return result;
}

+ (void) fireBlock:(KxSMBBlock)block withResult:(id)result
{
dispatch_queue_t queue = [KxSMBProvider sharedSmbProvider].completionQueue;
if (queue) {
dispatch_async(queue, ^{ block(result); });
} else {
block(result);
}
}

#pragma mark - internal methods

- (void) dispatchSync: (dispatch_block_t) block
Expand All @@ -1390,16 +1401,13 @@ - (void) fetchAtPath:(NSString *)path
{
NSParameterAssert(path);
NSParameterAssert(block);

dispatch_async(_dispatchQueue, ^{

id result = [KxSMBProvider fetchAtPath:(path.length ? path : @"smb://")
expandDir:expandDir
auth:auth];

dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
});
}

Expand Down Expand Up @@ -1446,9 +1454,7 @@ - (void) createFileAtPath:(NSString *)path
dispatch_async(_dispatchQueue, ^{

id result = [KxSMBProvider createFileAtPath:path overwrite:overwrite auth:auth];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
});
}

Expand All @@ -1475,9 +1481,7 @@ - (void) removeAtPath:(NSString *)path
dispatch_async(_dispatchQueue, ^{

id result = [KxSMBProvider removeAtPath:path auth:auth];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
});
}

Expand All @@ -1503,9 +1507,7 @@ - (void) createFolderAtPath:(NSString *)path
dispatch_async(_dispatchQueue, ^{

id result = [KxSMBProvider createFolderAtPath:path auth:auth];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
});
}

Expand Down Expand Up @@ -1807,9 +1809,7 @@ - (void) renameAtPath:(NSString *)oldPath
dispatch_async(_dispatchQueue, ^{

id result = [KxSMBProvider renameAtPath:oldPath newPath:newPath auth:auth];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
});
}

Expand Down Expand Up @@ -1982,9 +1982,7 @@ - (void) fetchItems:(KxSMBBlock) block
[provider dispatchAsync: ^{

id result = [KxSMBProvider fetchTreeAtPath:path auth:auth];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
}];
}

Expand Down Expand Up @@ -2314,9 +2312,7 @@ - (void)readDataOfLength:(NSUInteger)length
[provider dispatchAsync:^{

id result = [p readDataOfLength:length];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
}];
}

Expand All @@ -2341,9 +2337,7 @@ - (void)readDataToEndOfFile:(KxSMBBlock)block
[provider dispatchAsync:^{

id result = [p readDataToEndOfFile];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
}];
}

Expand All @@ -2370,9 +2364,7 @@ - (void)seekToFileOffset:(off_t)offset
[provider dispatchAsync:^{

id result = [p seekToFileOffset:offset whence:whence];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
}];
}

Expand All @@ -2399,9 +2391,7 @@ - (void)writeData:(NSData *)data
[provider dispatchAsync:^{

id result = [p writeData:data];
dispatch_async(dispatch_get_main_queue(), ^{
block(result);
});
[KxSMBProvider fireBlock:block withResult:result];
}];
}

Expand Down

0 comments on commit eed7681

Please sign in to comment.