We have an Android app. The Android App uses Volley (https://developer.android.com/training/volley/index.html) to transmit network data.
The point though is not about Volley, is about the pattern Volley uses to execute a remote call. Example:
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
As I said the point is not about Volley but about the pattern. I create a new Request, I put the callback in place, and when the call has finished the callback updates the UI.
Really simple.
Now, let's say that the user keeps clicking the damn button, I could generate a lot of calls. I would like to avoid that.
One solution, assuming no concurrency issues since click on the buttons are serial, would be to pass around a boolean
variable that will reset to true when the callback has completed.
I don't like that solution because it coupled things a lot.
Of course this code is really simple, in production you could have a more complex class structure.
What do you think could be a better solution?