Hi,
Imagine a table view listing some recipes. Each time the user taps on a recipe a new table view is loaded listing receipe ingredients.
To get the information, I'm asynchronous calling a REST API using:
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
When user taps on a recipe, a call to the API is made to get recipe ingredients. However, once in the new view (which list ingredients) and before the answer is received, the user can go back and select a new recipe.
In this case, I'll recive two answer; one for each request. The problem is I don't know to what request is this answer for and I'll refresh UI with a wrong content from a wrong answer.
I'm not sure which is the right approach in this case. I'm thinking about including in the answer each request parameter. So, if I'm, for example, using the API to search for certain term, we say "foo", I'll include the term in the answer too, for example:
Request:
http://domain.com/api/search?term=foo
Answer
{
"requestType": "search",
"term": "foo",
"result" : "a foo result"
}
It looks strange to me to include each request parameter on each answer, but it is the only solution I found to create a stateless API ready to be called asynchronous.
¿Is there any other way of accomplishing this?
(I'm using a delegate pattern assigning an object to each request which is called when the answer is received. The problem is, in the example of recipes, that the ingredients table view is reused eache time recipe ingredientes are listed).