Skip to content

Instantly share code, notes, and snippets.

@Mozilla9
Last active April 17, 2019 16:53
Show Gist options
  • Save Mozilla9/1530db57f21f860821a2 to your computer and use it in GitHub Desktop.
Save Mozilla9/1530db57f21f860821a2 to your computer and use it in GitHub Desktop.
- (void)saveWebDataInternal:(id)data completion:(void(^)(id, NSError *))completion
{
    NSBlockOperation *op = [[NSBlockOperation alloc] init];
    __block id result = nil;
    __block NSError *error = nil;

    op.completionBlock = ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(result, error);
        });
    };

    [op addExecutionBlock:^{
        dispatch_semaphore_t mutex = dispatch_semaphore_create(0);

        [self asyncImport:data completion:^(id importResult, NSError *importError) {
            result = importResult;
            error = importError;
            dispatch_semaphore_signal(mutex);
        }];

        dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER);
    }];

    // start operation
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [op start];
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment