当基础框架不支持排队时如何排队请求? [英] How to queue requests when underlying framework does not support queueing?

查看:90
本文介绍了当基础框架不支持排队时如何排队请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我的问题确实是n00b;抱歉,如果我说不清楚:/

I feel my question is really n00b; apologies if I don't word it clearly :/

在我的项目中,我使用的是第3方框架,该框架使用异步 NSURLRequests (RESTful API)为我获取了一些数据.接收数据并准备就绪后,将调用委托函数 didReceiveResponse .在发生错误的情况下,将调用 didFailWithError .

In my project I'm using a 3rd party framework that fetches some data for me using asynchronous NSURLRequests (RESTful API). When the data is received and ready, delegate function didReceiveResponse is called. On error case didFailWithError is called.

问题是:框架无法将请求排队,因此,如果我依次调用请求方法,我只会收到对最后发出的请求的响应.

The problem is: framework cannot queue requests, so if I call request methods sequentially I only receive the response for the last request issued.

现在的问题是我无法更改框架的代码.鉴于此,有什么办法可以解决此问题?像这样:对于一组请求,直到请求 n 完成后才发出 n + 1 请求( didReceiveResponse didFailWithError被调用);另外,当集合中的最后一个请求完成时,我会收到某种通知.理想情况下,该解决方案应在iOS3和iOS4上都受支持.

Now the issue is that I can't change framework's code. Given this is there any way of having a workaround for this problem? Something like: for a set of requests the n+1 request is not issued until request n is finished (either didReceiveResponse or didFailWithError was called); additionally, when the last request in a set is finished, I get a sort of notification. Ideally, the solution should by supported work for both iOS3 and iOS4.

推荐答案

似乎唯一的选择是控制启动请求的方法.为此,我将期望请求的概念抽象到其自己的对象或工厂中:将其称为 Command SingleRequestFactory 或类似名称.将这些对象放在一个数组中,当您收到 didReceiveResponse didFailWithError 时,请使下一个 Command 出队,并使用它构造下一个请求,您将其传递给第三方框架.如果您收到 didReceiveResponse didFailWithError 并且队列为空,请发布通知.

It seems like your only option is to control the method by which you start the requests. To that end, I would abstract the idea of a desired-request into an object or factory of its own: call it Command, or SingleRequestFactory, or whatever. Hold these objects in an array, and when you receive a didReceiveResponse or didFailWithError, dequeue the next Command and use it to construct the next request, which you pass along to your third-party framework. If you get a didReceiveResponse or didFailWithError and the queue is empty, post your notification.

最好创建一个自己执行所有这些逻辑的对象,以便您的视图控制器/抛出这些请求的任何人都可以告诉该对象将 Command 排队不用自己排队.

It'd probably be best to create an object that does all of this logic on its own, so that your viewcontrollers/whatever is throwing out these requests can just tell the object to queue a Command without holding the queue itself.

这篇关于当基础框架不支持排队时如何排队请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆