我正在使用正在进行异步网络调用的代理。响应将被处理并作为参数传递给下一行中的方法。代码插图低于:在主线程上等待,直到完成一个异步过程?
- (MyClass *) performOperations{
...
...
[self callProxyMethodsTarget:self action:@selector(receivedValue:) withObject:anObject]; //making an asynchronous call here
return response;
}
- (void) receivedValue:(id)response{ ///need this return value from network call to return from performOperartions
...
...
}
我的问题是,作为代理方法进行异步网络调用,我不能够及时得到响应返回。
我已经想到了很多事情,比如:
- 使网络调用同步但这需要大量的返工,并可能引入不可预见的问题
- 改变返回类型,并通过从receivedValue响应,但不能这样做,因为我的客户已经定义了这些方法的名称和返回类型以及其他平台上的团队使用的是同步调用
我还以为像一个解决方案:
- (MyClass *) performOperations{
...
...
[self callProxyMethodsTarget:self action:@selector(receivedValue:) withObject:anObject]; //making an asynchronous call here
///write a code here to wait until response is received
return response;
}
- (void) receivedValue:(id)response{ ///need this return value from network call to return from performOperartions
...
...
}
但未能如愿。
确定代理方法有委托方法(我声明名称为)receivedValue:表示网络调用已完成。我不明白你在答案中所说的内容,你能否用代码稍微演示一下。您可以将我的代码修改为演示。 – 2010-12-16 06:32:43
问题是在返回类型我的朋友。在我的代码执行操作返回的东西,但在你的代码performOperations有一个void返回类型:( – 2010-12-16 07:28:05
了解,这就是为什么我把'receivedValue'的评论,在那一点上,你将返回值的两种方式之一。第一种选择是向类'performOperations'和'receivedValue'中添加委托协议,将数据发送回调用方,如'[[self perfOpsDelegate] operationSuccessful:YES withResponseValue:response];'。另一个选项是使用NSNotificationCenter通知:调用者观察类似于ProxyDataUpdatedNotification的内容,'receivedValue'在通知的'userInfo'字典中用'response'发送该通知。 – 2010-12-16 22:07:50