有人可以告诉我为什么block不等待objc处理程序?

时间:2015-11-05 11:59:32

标签: ios iphone grand-central-dispatch

再次,关于GCD的问题。我不知道为什么他们被发明,因为他们没有正常工作或我误解了什么。

我有一些controller用于添加新优惠券。它负责将详细信息保存到Core Data中。然后ImagesCollection用于获取一些imgs并用于将UIImage转换为NSData

它看起来像那样:

- (IBAction)saveButtonClicked:(id)sender {
__weak typeof(self) weakSelf = self;
__block NSSet *imgs = nil;

if (!imagesCollection) 
    imgs = [self.coupon couponImages];

[imagesCollection saveImagesWithCompletion:^(NSSet *images, NSError *error) {

    if (error) {
        [[[UIAlertView alloc] initWithTitle:error.localizedDescription
                                    message:error.localizedFailureReason
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil] show];
    } else {
        imgs = [images copy];
    }
}];

------- PhotosCollection --------
@interface...
typedef void (^ImagesCompletionBlock)(NSSet *images, NSError *error);
-(void)saveImagesWithCompletion:(ImagesCompletionBlock)completion;

@implementation...
ImagesCompletionBlock _block;

-(void)saveImagesWithCompletion:(void (^)(NSSet *images, NSError *error))success {
_block = [success copy];
NSError *error = nil;


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    NSMutableSet *processedImages = [NSMutableSet new];

    for (UIImage *image in images) {
        [processedImages addObject:[NSData dataWithData:UIImagePNGRepresentation(image)]];
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        if (_block) {
            _block([processedImages copy],error);
        }
    });
});
}

如何不等待完成处理程序被触发?

感谢您的帮助, 沮丧 - 阿德里安。

0 个答案:

没有答案