在 iOS 中的异步 nsurlsession 中调用同步 nsurlsession
Call sync nsurlsession inside async nsurlsession in iOS
我正在尝试在异步 nsurlsession 调用中调用同步 nsurlsession 调用,但它不起作用。同步呼叫未完成。下面是代码。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL1"]];
[request setHTTPMethod:@"GET"];
// Call first service
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(data){
// If first webservice is successful then dont call second service
return;
}
// Call second service
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL2"]];
[request2 setHTTPMethod:@"GET"];
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[NSURLSession.sharedSession dataTaskWithRequest:request2 completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_semaphore_signal(sem);
}] resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}] resume];
正如@Larme 所提到的,最好将完成块传递到您的方法中,然后在任一请求的完成块中调用它(看起来您想使用第一个 Web 服务中的数据)有一些,如果没有则第二个):
- (void)webServiceRequestWithCompletion:(void (^_Nullable)(NSData *_Nullable completionData, NSError *_Nullable error))completionAfter {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL1"]];
[request setHTTPMethod:@"GET"];
// Call first service
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
// If first webservice is successful then dont call second service
if (completionAfter) {
completionAfter(data, error);
}
return;
}
// Call second service
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL2"]];
[request2 setHTTPMethod:@"GET"];
[[NSURLSession.sharedSession dataTaskWithRequest:request2 completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completionAfter) {
completionAfter(data, error);
}
}] resume];
}] resume];
}
然后你可以这样调用它:
[self webServiceRequestWithCompletion:^(NSData *completionData, NSError *error) {
// do stuff with data returned from one of the two webservices here
}];
如果这不是您想要做的,那么您需要编辑您的问题并提供额外的上下文,例如如何调用此代码以及为什么您必须使用信号量(它在等待什么)上?)
此站点有助于了解如何使用块语法:http://goshdarnblocksyntax.com
我正在尝试在异步 nsurlsession 调用中调用同步 nsurlsession 调用,但它不起作用。同步呼叫未完成。下面是代码。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL1"]];
[request setHTTPMethod:@"GET"];
// Call first service
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(data){
// If first webservice is successful then dont call second service
return;
}
// Call second service
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL2"]];
[request2 setHTTPMethod:@"GET"];
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[NSURLSession.sharedSession dataTaskWithRequest:request2 completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_semaphore_signal(sem);
}] resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}] resume];
正如@Larme 所提到的,最好将完成块传递到您的方法中,然后在任一请求的完成块中调用它(看起来您想使用第一个 Web 服务中的数据)有一些,如果没有则第二个):
- (void)webServiceRequestWithCompletion:(void (^_Nullable)(NSData *_Nullable completionData, NSError *_Nullable error))completionAfter {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL1"]];
[request setHTTPMethod:@"GET"];
// Call first service
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
// If first webservice is successful then dont call second service
if (completionAfter) {
completionAfter(data, error);
}
return;
}
// Call second service
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL2"]];
[request2 setHTTPMethod:@"GET"];
[[NSURLSession.sharedSession dataTaskWithRequest:request2 completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completionAfter) {
completionAfter(data, error);
}
}] resume];
}] resume];
}
然后你可以这样调用它:
[self webServiceRequestWithCompletion:^(NSData *completionData, NSError *error) {
// do stuff with data returned from one of the two webservices here
}];
如果这不是您想要做的,那么您需要编辑您的问题并提供额外的上下文,例如如何调用此代码以及为什么您必须使用信号量(它在等待什么)上?)
此站点有助于了解如何使用块语法:http://goshdarnblocksyntax.com