Reputation: 512
Here's the situation, I have 2 classes, a class for my fragment and a singleton to get my objects. Every time this fragment is created, it will ask my singleton to create objects using JSON from my server using Volley. However, this resulting in my singleton returning the objects before it is completely created. This means my fragment will display nothing, except in some cases that the HTTP request finish first. (I tried logging, and indeed the method calls are not in order)
What is a good approach so I can update my view after all HTTP requests are completed? (I think of passing object of fragment to my Factory, and calling my fragment's method in onResponse, but I guess it will get messy if I need my Factory for other classes?)
public class myFragment extends Fragment{
onCreate(){
...
UpdateUI();
}
private void updateUI(){
Factory factory = factory.get(getActivity());
things = pabrik.getThings();
...
}
}
public class Factory{
Factory factory;
List<Things> things;
private Factory(Context c){
things = new ArrayList<>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "10.0.2.2/...", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray arr = response.getJSONArray("user");
for (int i = 0; i < arr.length(); i++){
things.add(new Barang(arr.getJSONObject(i).getString("email"), i));
Log.d("GET", arr.getJSONObject(i).getString("email"));
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
...
);
mRequestQ.add(request);
}
public static synchronized Factory get(Context c){
if(this.factory == null){
this.factory = new Factory(c);
}
return this.factory;
}
public List<Things> getThings(){
return this.Things;
}
}
Upvotes: 0
Views: 287
Reputation: 342
The primary use case of volley is to do networking from the main(event) thread. So do whatever you're doing in your singleton on the onResponse method from the main thread. Volley ensures that network task is run on a separate background thread and calls the onResponse after the background has finished executing.
public class myFragment extends Fragment{
onCreate(){
...
UpdateUI();
}
private void updateUI(){
Factory factory = factory.get(getActivity());
things = new ArrayList<>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "10.0.2.2/...", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray arr = response.getJSONArray("user");
for (int i = 0; i < arr.length(); i++){
things.add(new Barang(arr.getJSONObject(i).getString("email"), i));
Log.d("GET", arr.getJSONObject(i).getString("email"));
//Update ListView or do something with the data
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
...
);
mRequestQ.add(request);
...
}
}
PS: If you want to use a singleton, you should pass a listener interface and work with handlers and loopers. Handler does the task in the background thread and then updates main thread through the object implementing the interface. The purpose of volley is to avoid these complications.
Upvotes: 1