Reputation: 83
I want to run jobs in serial queue(Wait for first job to start second). I am using android priority queue library which allows you to run jobs in serial by setting same group id but it dont work in my case.
I have added three jobs in queue
jobManager.addJobInBackground(new FetchQuestionsJob(this)); jobManager.addJobInBackground(new FetchUsersJob(this)); jobManager.addJobInBackground(new FetchTeamsJob(this));
My all three jobs are similar to this class but all of the jobs run concurrently. I receive response from FetchUsersJob/FetchTeamsJob earlier than FetchQuestionsJob.
public class FetchQuestionsJob extends Job{
Context context;
public FetchQuestionsJob(Context context){
super(new Params(9).requireNetwork().setGroupId(FETCH_REQUESTS));
this.context = context;
}
@Override
public void onAdded() {
}
@Override
public void onRun() throws Throwable {
new FetchQuestionsApi(context);
}
@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
}
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
return null;
}
FetchQuestionApi
public class FetchQuestionsApi implements IDataReceiveListener {
VolleyNetworkController networkController;
Context context;
Realm realm;
public FetchQuestionsApi(Context context) {
this.context = context;
networkController = new VolleyNetworkController(context);
networkController.getRequest(URL_GET_QUESTIONS, null, null, this);
}
@Override
public void onDataReceived(JSONObject jsonObject) {
try {
if (jsonObject.getBoolean(RESPONSE_SUCCESS)) {
JSONArray data = jsonObject.getJSONArray("Data");
Gson gson = new Gson();
Question[] question = gson.fromJson(data.toString(), Question[].class);
realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(Arrays.asList(question));
realm.commitTransaction();
Question specificCountry = realm.where(Question.class).findFirst();
String id = specificCountry.getId();
Log.d("", jsonObject.toString());
AppController.getInstance().getJobManager().addJobInBackground(new FetchUsersJob(context));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void OnError(String message) {
}
Upvotes: 1
Views: 373
Reputation: 3773
Try to use .groupBy(FETCH_REQUESTS)
instead of .setGroupId(FETCH_REQUESTS)
. It works fine in my case.
Upvotes: 0