Ios 是否将块调用添加到NSArray?
我使用这段代码截获类“NSURLConnection”的方法“sendAsynchronousRequest”,并尝试在完成处理程序中执行一些代码Ios 是否将块调用添加到NSArray?,ios,handler,objective-c-blocks,Ios,Handler,Objective C Blocks,我使用这段代码截获类“NSURLConnection”的方法“sendAsynchronousRequest”,并尝试在完成处理程序中执行一些代码 void (*oldHandler)(id,NSURLResponse*, NSData*, NSError*); void myHandler(id block, NSURLResponse *response, NSData *data, NSError *error ) { NSLog( @"myHandler: %@ %@
void (*oldHandler)(id,NSURLResponse*, NSData*, NSError*);
void myHandler(id block, NSURLResponse *response, NSData *data, NSError *error ) {
NSLog( @"myHandler: %@ %@", response, error );
oldHandler( block, response, data, error );
}
…
[object_getClass([NSURLConnection class]) beforeSelector:@selector(sendAsynchronousRequest:queue:completionHandler:)
callBlock:^( NSURLConnection *url, SEL _cmd, id request, NSOperationQueue *queue, id handler ) {
struct _block {
Class isa;
int flags;
int reserved;
void (*invoke)();
} *blockHandler = (__bridge struct _block *)handler;
oldHandler = blockHandler->invoke;
blockHandler->invoke = myHandler;
}];
问题是,如果在我的应用程序中有两个对“sendAsynchronousRequest”的连续调用,我将失去对第一个调用的调用,因为“oldHandler”将随着该方法的第二次调用而改变
我曾尝试将oldHandler添加到NSArray或NSDictionary中,以标识“myHandler”方法中的每个调用,但没有成功
你知道解决我问题的办法吗
谢谢为什么不将代码移到完成处理程序中?我在一个框架上工作,因此无法添加完成处理程序,因为它位于客户端应用程序端。我想我将创建NSURLConnection的子类,即SubassofnSurlConnection,并覆盖SendaySnChrousRequest。我将要求用户使用subassofnsurlconnection而不是NSURLConnection。为什么不将
oldHandler
(作为块)作为第一个参数传递给myHandler
,在myHandler
中只调用块,而不需要对块头执行任何疯狂的操作呢?