尝试使用此方法从iOS应用程序进行rest api调用http://spring.io/guides/gs/consuming-rest-ios/ 需要一个接一个地进行3个api调用,并且需要在随后的api调用中使用部分json结果。我该怎么办? iOS应用程序是使用Objective-C
开发的答案 0 :(得分:1)
设置一个这样的后台线程:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
<#code#>
});
并在其中同步执行您的API调用。它可能看起来像这样:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
id dataOne = [self apiCallOne];
id dataTwo = [self apiCallTwoWithDataOne:dataOne];
id dataThree = [self apiCallThreeWithDataTwo:dataTwo];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Completion with data: %@", dataThree);
});
});
请务必使用[NSURLConnection sendSynchronousRequest: ...];
而不是[NSURLConnection sendAsynchronousRequest: ...];