简体   繁体   中英

completion block in background thread

I have a method that does some time extensive data extraction that I'd like executed in the background. However, I need to return a UIBezierPath when the block completes before moving forward.

I'm trying to do this:

__block UIBezierPath *blockWavePath = nil;

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{

    dispatch_async(dispatch_get_main_queue(), ^{
        blockWavePath = [pathGenerator createPathWithAsset:audioAsset andSize:myCell.waveView.bounds.size];
    });
});

How can I incorporate the completion check aspect into this?

I think you should use completion block here, completion blocks are for callback code. Like in your case when you want to do something after some work is completed.

In your case, it should be something like this

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{

    dispatch_async(dispatch_get_main_queue(), ^{
        [self createPathWithAsset:audioAsset andSize:0 completionBlock:^(UIBezierPath* resultValue){
            blockWavePath = resultValue;
        }];
    });
});

Where your createPathWithAsset should look something like below

-(void) createPathWithAsset: (AVAsset *) asst  andSize : (NSInteger) Size completionBlock: (void (^) (UIBezierPath *)) completionB {


    UIBezierPath *someBezeirPathObject;   //example object
    //Your function implementation goes here
    //.
    //.
    //.


    //When your functionality is achieved, pass the object to completion block
    completionB(someBezeirPathObject);


}

For better understanding on completion block, I would suggest this Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM