在Android优先级队列

时间:2017-04-10 13:42:52

标签: android multithreading queue jobs android-priority-jobqueue

我想在串行队列中运行作业(等待第一个作业开始第二个)。我正在使用android优先级队列库,它允许你通过设置相同的组ID来串行运行作业,但在我的情况下它不起作用。

我在队列中添加了三个作业

  

jobManager.addJobInBackground(new FetchQuestionsJob(this));           jobManager.addJobInBackground(new FetchUsersJob(this));           jobManager.addJobInBackground(new FetchTeamsJob(this));

我的所有三个工作都与此课程相似,但所有工作都同时进行。我在FetchQuestionsJob之前收到来自FetchUsersJob / FetchTeamsJob的回复。

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

  

公共类FetchQuestionsApi实现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) {

}

1 个答案:

答案 0 :(得分:0)

尝试使用.groupBy(FETCH_REQUESTS)代替.setGroupId(FETCH_REQUESTS)。它在我的情况下工作正常。