我有一个调用长时间运行进程的方法。长时间运行的过程,我使用AFNetworking,它本身使用块并返回成功块和故障块。所以我试图测试我的方法,并且在调用成功块之前测试将失败。我以为我会尝试让我的方法也使用块。我有另一种方法,我设法更改为使用块,但只使用bool isFinished,返回值为void。我难以处理的方法需要返回NSDecimalNumber *并采用NSString。
方法签名
我希望能够添加一个带有我在AFNetworking方法进入成功块时设置的BOOL的完成块
我还希望能够调用该方法并在其完成块内访问它返回的NSDecimalNumber *值
可能?如果是这样,请告诉我如何
答案 0 :(得分:3)
你可能不得不分开它。
您可以使用fetchRate:
方法获取完成块:
- (void)fetchRate:(NSString*)rateCode completion:(void (^)(NSDecimalNumber *))completion;
然后这样打电话:
void (^completion)(NSDecimalNumber *) = ^(NSDecimalNumber * rate){
// this is called when rate is returned from your webservice
}
// call fetchRate: now, results will arrive later...
[ myObj fetchRate:<rate code> completion:completion ];
// code here runs immediately; the results come back later.
-fetchRate:
看起来像......
- (void)fetchRate:(NSString *)rateCode completion:(void (^)(NSDecimalNumber *))completion
{
void (^asiCompletionBlock)(/*args*/) = ^(/*...args...*/){
// called after ASI request completes
NSDecimalNumber * answer = /* get answer from ASI HTTP response */
// call our completion block that was passed in:
completion( answer );
};
// do your asi HTTP request here, pass asiCompletionBlock for completion arg
}