如何将目标动作转换为阻止?

时间:2013-03-29 23:05:33

标签: ios objective-c objective-c-blocks

我在类中有以下选择器:

    - (SoapRequest*) loginAndConnect: (id) target action: (SEL) action credentials: (WSAcredentials*) arg0 dialoutInfo: (WSAdialoutInfo*) arg1;

我希望能够用一个块而不是另一个选择器来调用它作为动作,但我找不到有关如何执行此操作的信息。类似的东西:

[service loginAndConnect:self  credentials:credentials dialoutInfo:dialoutInfo action:^(SoapRequest* aResult) {
    // more code here
}];

实现这一目标的最佳方法是什么?

UPDATE * *

我几乎有这个工作但是在objc_release中为completionBlock对象获得了一个异常。我确定它与保留目标有关,但我不确定如何纠正它。这是当前的代码:

- (BOOL) checkService {

WSAcredentials* credentials = [[WSAcredentials alloc] init];
WSAdialoutInfo* dialoutInfo = [[WSAdialoutInfo alloc] init];
if (!service) {
    service = [[WSAWebSocketAdapterService alloc] init];
}
__block SoapRequest* request;
[service loginAndConnectWithCredentials:credentials dialoutInfo:dialoutInfo completionBlock:^(SoapRequestCompletionBlock completionBlock) {
        request = (SoapRequest*)completionBlock;
        if ([request isKindOfClass: [SoapFault class]]) {                
            return YES; // we got a response, that's all we care about
        }
    return NO;
}
];
return YES;
}

这是我的类别,非常接近下面发布的内容:

#import "WSAWebSocketAdapterService+BlockExtension.h"

// These objects serve as targets (with action being completed:) for the original object.
// Because we use one for each request we are thread safe.

@interface MyCustomSoapTargetAction : NSObject 

@property (copy) SoapRequestCompletionBlock block;

- (void) completed:(id)sender;

@end

@implementation MyCustomSoapTargetAction
- (void) completed:(id)sender
{
// Assuming 'sender' is your SoapRequest
if (_block != nil)
    _block(sender);
_block = nil;
}
@end

@implementation WSAWebSocketAdapterService(BlockExtension)

- (SoapRequest*) loginAndConnectWithCredentials:(WSAcredentials*) arg0
                                dialoutInfo: (WSAdialoutInfo*) arg1
                            completionBlock:(BOOL (^)(SoapRequestCompletionBlock))completionBlock
{
MyCustomSoapTargetAction *target = [[MyCustomSoapTargetAction alloc] init];
target.block = (SoapRequestCompletionBlock) completionBlock;

//
// Here we assume that target will be retained.
// If that's not the case then we will need to add it to some collection before
// the call below and have the target object remove itself from it after its
// block has been called.
//
return [self loginAndConnect:target action:@selector(completed:) credentials:arg0 dialoutInfo:arg1];
}
@end

感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

您实际上不需要源代码。一个类别会做得很好。这里有一些示例代码可以帮助您入门, 这甚至是线程安全的!如果你的对象没有保留目标,那么还有一些工作要做,以便在调用它的块之前保持它。

// -- TheClassName+BlockExtension.h

typedef void (^SoapRequestCompletionBlock)(SoapRequest*);

@interface TheClassName(BlockExtension)
- loginAndConnectWithCredentials:(WSAcredentials*) arg0 
                     dialoutInfo: (WSAdialoutInfo*) arg1;
                 completionBlock:(SoapRequestCompletionBlock)completionBlock;
@end


// -- TheClassName+BlockExtension.m

// EDITED: If the 'target' object is not retained by the loginAndConnect... then
//         we may add it to this collection. In this implementation, a block can
//         only be used once.

static NSMutableSet *gSoapTargets = nil;

// These objects serve as targets (with action being completed:) for the original object.
// Because we use one for each request we are thread safe.

@interface MyCustomSoapTargetAction
@property (copy) SoapRequestCompletionBlock block;
- (void) completed:(id)sender;
@end

@implementation MyCustomSoapTargetAction

- (id) init
{
    // Objects adds itself to the global collection
    if ((self = [super init]))
        [gSoapTargets addObject:self];
    return self;
}

- (void) completed:(id)sender
{
    // Assuming 'sender' is your SoapRequest
    if (_block != nil)
        _block(sender);

    // Object removes itself from global collection when done.
    // On return from this method it will likely be released.
    [gSoapTargets removeObject:self];
}

+ (void) load 
{
    gSoapTargets = [NSMutableSet set];
}

@end

@implementation TheClassName(BlockExtension)
- (SoapRequest*) loginAndConnectWithCredentials:(WSAcredentials*) arg0 
                                    dialoutInfo: (WSAdialoutInfo*) arg1;
                                completionBlock:(SoapRequestCompletionBlock)completionBlock
{
    MyCustomSoapTargetAction *target = [[MyCustomSoapTargetAction alloc] init];
    target.block = completionBlock;

    //
    // Here we assume that target will be retained.
    // If that's not the case then we will need to add it to some collection before
    // the call below and have the target object remove itself from it after its 
    // block has been called.
    //
    [self loginAndConnect:target action:@selector(completed:) credentials:arg0 dialoutInfo:arg1];
}
@end

* 更新:以下是根据您的代码使用此类别的示例:

- (BOOL) serviceIsAvailable 
{
    return _serviceIsAvailable;
}

- (void) _setServiceIsAvailable:(BOOL)value 
{
    // This method should probably be private thus the _ prefix
    // Do something with the result (set some status, warn user...).
    _serviceIsAvailable = value;
}

- (void) checkService 
{
    WSAcredentials* credentials = [[WSAcredentials alloc] init];
    WSAdialoutInfo* dialoutInfo = [[WSAdialoutInfo alloc] init];

    if (_service == nil)
        _service = [[WSAWebSocketAdapterService alloc] init];

    [_service loginAndConnectWithCredentials:credentials 
                                 dialoutInfo:dialoutInfo 
                             completionBlock:^(id sender) 
    {
        [self _setServiceIsAvailable:[sender isKindOfClass:[SoapFault class]]];
    }];
}

答案 1 :(得分:0)

您是否可以访问该课程的来源?生成一个接受块而不是选择器的类似方法将是一个相当简单的重写。

AFNetworking库是一种很好的风格指南。您的方法可能与此类似:

+ (instancetype)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
                                        success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
                                        failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure;

或者,您可以使用此处演示的方法通过类别添加您的方法:Blocks instead of performSelector:withObject:afterDelay: