Wait for assetForURL blocks to be completed

前端 未结 5 2095
离开以前
离开以前 2020-12-23 11:57

I would like to wait this code to be executed before to continue but as these blocks are called assynchronously I don\'t know how to do???

NSURL *asseturl;
N         


        
5条回答
  •  清酒与你
    2020-12-23 12:57

    GCD semaphore approach:

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    
    for (NSURL *url in self.assetUrls) {
        dispatch_async(queue, ^{
            [library assetForURL:url resultBlock:^(ALAsset *asset) {
                [self.assets addObject:asset];
                dispatch_semaphore_signal(sema);
            } failureBlock:^(NSError *error) {
                dispatch_semaphore_signal(sema);
            }];
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    dispatch_release(sema);
    
    /* Check out ALAssets */
    NSLog(@"%@", self.assets);
    

提交回复
热议问题