1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-04 18:37:47 +01:00
Telegram/thirdparty/GDFileManager/GDDispatchUtilities.m

62 lines
2.3 KiB
Mathematica
Raw Normal View History

2015-10-01 18:19:52 +02:00
#import "GDDispatchUtilities.h"
typedef void(^ContinuationBlock)(BOOL);
void __attribute__((overloadable)) AsyncSequentialEnumeration(NSEnumerator *enumerator,
void (^iterationBlock)(id object, ContinuationBlock continuationBlock))
{
return AsyncSequentialEnumeration(enumerator, iterationBlock, NULL);
}
void __attribute__((overloadable)) AsyncSequentialEnumeration(NSEnumerator *enumerator,
void (^iterationBlock)(id object, ContinuationBlock continuationBlock),
void (^completionBlock)(BOOL completed))
{
return AsyncSequentialEnumeration(enumerator, NULL, iterationBlock, completionBlock);
}
void __attribute__((overloadable)) AsyncSequentialEnumeration(NSEnumerator *enumerator,
dispatch_queue_t queue,
void (^iterationBlock)(id object, ContinuationBlock continuationBlock),
void (^completionBlock)(BOOL completed))
{
if (!queue) {
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}
#if !OS_OBJECT_USE_OBJC
dispatch_retain(queue);
#endif
__block void (^continuationBlock)(BOOL) = ^(BOOL keepGoing){
if (!keepGoing) {
if (completionBlock) {
dispatch_async(queue, ^{
completionBlock(NO);
});
}
#if !OS_OBJECT_USE_OBJC
dispatch_release(queue);
#endif
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
continuationBlock = nil;
#pragma clang diagnostic pop
return;
}
id object = [enumerator nextObject];
if (object && continuationBlock) {
dispatch_async(queue, ^{
iterationBlock(object, continuationBlock);
});
} else {
if (completionBlock)
completionBlock(YES);
#if !OS_OBJECT_USE_OBJC
dispatch_release(queue);
#endif
continuationBlock = nil;
}
};
continuationBlock(YES);
}