0

我想在串行队列中运行作业(等待第一个作业开始第二个)。我正在使用 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) {

}
4

1 回答 1

0

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

于 2018-02-07T17:55:07.340 回答