首先是反向传值。 代理传值和Block结合。
(一)首先 NSURL delegate拿到返回值。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
...
[self operationSucceeded]; //启动MK处理方法,
...
}
(二)MKNetworkOperation 拿到数据 启动Block 回调传值
-(void) operationSucceeded { // 注意mk是个数组中存放的blocks,因为是队列机制
for(MKNKResponseBlock responseBlock in self.responseBlocks)
responseBlock(self); // CSIINetworkHttpClient中定义的此Block的代码块函数体
}
(三) CSIINetworkHttpClient 被responseBlock的代码块函数体 回调传值
onSuccessBlock([completedOperation responseString],webInfo);//这个Block是调用Client页面CSIIBusinessTransaction传进来的onSuccessBlock代码块,在此处被传值调用。
{
(四) CSIIBusinessTransaction 被Block 回调传值
(1) CSIINetworkHttpClient 被实例化,并实现了onSuccessBlock代码块此Block在下面函数中被实现代码块
client=[[CSIINetworkHttpClient alloc]initWithTransaction:self.caller.transactionId message:self.caller.transactionArgument caller:self.caller argument:nil onSuccessBlock:^(NSString *string,NSMutableDictionary *webInfo) {
(2) /*
* lcxNote 此处直接传值给了 caller.webdata.
**/
self.caller.webData = [string objectFromJSONString];
(3) [self.delegate transactionSucceeded:self]; //通过此方法代理高给CSIIBusinessLogic 启动Caller的VC页面,告知VC页面可以
,不再是Block
webData
}
(五) CSIIBusinessLogic 单例类 在executeTransaction中,启动了CSIIBusinessTransaction并成为其代理代码如下。
[pageRunningTransactions addObject:[[CSIIBusinessTransaction alloc]initWithTransaction:self caller:caller]];
//lcxNote启动Transaction,并成为其代理,启动后,CSIIBusinessTransaction 最终从(第一步系统自带的)NSURLdele得到数据。
通过回调 transactionSucceeded 或得值,
- (void)transactionSucceeded:(CSIIBusinessTransaction*)transaction;{ if (transaction.caller.originalClass == object_getClass(transaction.caller.delegate)) { if (transaction.caller.transactionBlock) { transaction.caller.isSuccess = YES; if (transaction.caller.isWeb) { }else{ transaction.caller.transactionResult = transaction.resultObject;
}
transaction.caller.transactionBlock(transaction.caller); // 执行了ViewController中实现的caller定义的需要传入自身做参数的TransactionBlock ,并没有传值给Caller, caller是直接从Transaction中的on
}else{ if ([transaction.caller.delegate respondsToSelector:@selector(transactionCallback:)]) { transaction.caller.isSuccess = YES; if (transaction.caller.isWeb) { }else{ transaction.caller.transactionResult = transaction.resultObject; } [transaction.caller.delegate transactionCallback:transaction.caller]; } } } [self removeTransaction:transaction.caller]; if ([self.runningTransactions count]==0) { if (transaction.caller.isShowActivityIndicator) { //关闭转轮 [[CSIIUIActivityIndicator sharedInstance]close]; } }
}
其次是正向发起交易。
(一) 交易发起过程。
(1)实例化CSIIBusinessCaller
new_transaction_caller
/* * lcxNote CSIIBusinessCaller * caller = [[CSIIBusinessCaller alloc] init];\ caller.pageId = NSStringFromClass([self class]);\ caller.delegate = (id<CSIIBusinessCallerDelegate>)self; **/ caller.transactionId = GenToken; caller.isWeb = YES; __block NSString *tokenName = nil;
(2)CSIIBusinessLogic类启动CSIIBusinessTransaction
#define execute_transaction_block_caller(block) [[CSIIBusinessLogic sharedInstance]executeTransaction:caller transactionBlock:block];
// [[CSIIBusinessLogic sharedInstance]executeTransaction:caller transactionBlock:block]; execute_transaction_block_caller((^(CSIIBusinessCaller *returnCaller){
if ( returnCaller.isSuccess) {
tokenName = returnCaller.webData; DebugLog(@"lcxDebug 输入完手机号码后的 returnCaller.webData->%@\n",returnCaller.webData); _tokenName=tokenName;
} }));
}
(3)CSIIBusinessTransaction 启动了Client,Client启动了MK,MK使用NSURL获得数据,并反向传值。
(二) Caller中定义了一个TransactionBlock 它的参数是自身CSIIBusinessCaller,这个Block是VC页面获得数据的方式。
{ typedef void(^TransactionBlock)(CSIIBusinessCaller* returnCaller);
网友评论