开发者

How to queue requests when underlying framework does not support queueing?

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

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 req开发者_JS百科uest methods sequentially I only receive the response for the last request issued.

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.


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜