在我的应用程序中,单击登录按钮时,我们必须进行3次服务器调用。

1) will make a server call to get an OAuth token for user credentials
2) with the token from (1) we will get user privileges
3) with the token from (1) and with the valid privilege from (2) we will get the data to be displayed on the page after login in a tableview.

我对采取的方法感到困惑。使用操作队列添加依赖项或使用NSURLSession任务是一种好方法吗?

根据对此的一种堆栈溢出解决方案-Best practices for making a queue of NSURLSessionTasks
NSURLSession没有任何关于排序请求的逻辑,即使将每台主机的最大连接数设置为1,它也只会调用completelyBlock首先完成

如果还有其他更好的方法,请告诉我。

最佳答案

您可以使用NSURLSession任务。首先调用第一个api方法,您将在完成处理程序(块)中获得响应。现在将其存储在完成处理程序内的任何 public 属性(您想再次使用它)中。从该完成处理程序调用第二个api方法,并从第二个方法的完成处理程序,通过传递响应并使用存储了第一个api方法对象的 public 属性,调用第三个api方法。

完成处理程序或仅在响应到达时阻止调用,因此通过这种方式,您可以管理api调用。

希望这会有所帮助:)

10-07 19:49
查看更多