在一个使用AFNetworking进行服务调用的应用程序中,我有以下场景:
每次向服务器提出请求时,我都必须遵循这三个步骤。我不能改变服务器的工作方式,所以我必须遵守这个要求。对于多个请求,我也不能使用相同的令牌。
我的问题如下--我试图使用AFHTTPRequestOperation来完成这个任务:
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:@"serviceName.json" relativeToURL:self.manager.baseURL] absoluteString] parameters:@{ @"token": token } error:&serializationError];
AFHTTPRequestOperation *myRequestOperation = [self.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
// Success login
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
// Failure logic
}];
[myRequestOperation addDependency:createTokenRequestOperation];其中self.manager是AFHTTPRequestOperationManager的一个实例,但是有一个问题--我没有token的值。
由于myRequestOperation应该只在上面列表中的第1点之后执行,所以我使它依赖于将获得令牌的操作。
现在出现了我的困惑--当我需要实例化两个参数以使其中一个依赖于另一个操作时,如何创建一个使用以前操作中的参数的操作?
发布于 2015-10-06 18:03:08
由于无法找到适合我的解决方案,所以我最终使用了PromiseKit,它允许我链接如下异步调用:
[NSURLConnection promise:rq1].then(^(id data1){
return [NSURLConnection promise:rq2];
}).then(^(id data2){
return [NSURLConnection promise:rq3];
}).then(^(id data3){
// Work with the data returned from rq3
});https://stackoverflow.com/questions/32874103
复制相似问题