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

£可爱£侵袭症+ 提交于 2020-01-06 10:09:30

问题


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 request 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.


回答1:


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.



来源:https://stackoverflow.com/questions/5018762/how-to-queue-requests-when-underlying-framework-does-not-support-queueing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!