dispatch_semaphore_t信号量控制程序执行循序

阅读更多

 

dispatch_semaphore_t信号量控制程序执行循序

 

        //确保 读取相册资源完毕
        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
        //等待线程结束的信号量
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        dispatch_async(queue, ^(){
            //取得相片数量
            [[AssetHelp sharedAssetHelper] getGroupList:[ALAssetsFilter allPhotos] cb:^(NSArray * groupList) {
                ALAssetsGroup *group = [groupList firstObject];
                //初始化相册列表元素
                [[AssetHelp sharedAssetHelper] getPhotoListOfGroup:group result:^(NSArray *assetList) {
                    //缓存本地相册列表
                    photosLocalAssetList = [assetList copy];
                    NSLog(@"----0---");
                    //初始化图片数据
                    for (ALAsset *asset in assetList) {
                        MWPhoto *photo = [MWPhoto photoWithURL:asset.defaultRepresentation.url];
                        [photosLocalImport addObject:photo];
                        MWPhoto *thumb = [MWPhoto photoWithImage:[UIImage imageWithCGImage:asset.thumbnail]];
                        [thumbsLocalImport addObject:thumb];
                        //                    if ([asset valueForProperty:ALAssetPropertyType] == ALAssetTypeVideo) {
                        //                        photo.videoURL = asset.defaultRepresentation.url;
                        //                        thumb.isVideo = true;
                        //                    }
                    }
                    //标记执行结束
                    dispatch_semaphore_signal(sema);
                }];
            }];
        });
        
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        NSLog(@"----1---");

 

你可能感兴趣的:(Objective-C)