将块传递给iOS中的块

时间:2013-10-16 01:43:29

标签: ios automatic-ref-counting objective-c-blocks

我正在尝试创建一个接受完成处理程序块的包装器方法,然后将其传递给另一个完成处理程序。问题是,无论我做什么,我的完成处理程序在被调用之前都会被释放。这是删除大部分逻辑的方法。

+ (int) syncWithCompletion:(void (^)(NSError *error))completion;{
    //Copy the block argument and make it available to the block scope
    __block void (^completionBlock)(NSError *error) = [completion copy];
    // The is an AFNetworking operation
    [af.sharedHTTPClient enqueueBatchOfHTTPRequestOperations:syncOperations progressBlock:nil completionBlock:^(NSArray *operations) {
        // Call my copied block... but it's already been deallocated
        completionBlock(error);
    }];
    return somevariable;
}

1 个答案:

答案 0 :(得分:1)

bah ...代码正在死于另一个调用函数,其中完成处理程序为零。这修好了它:

if(completionBlock){
    completionBlock(error);
}