gpt4 book ai didi

android - 为什么异步方法在 Retrofit 2 中调用得更快?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:27 25 4
gpt4 key购买 nike

正如您在下面的代码中看到的,我调用了 getPostByPageId() 方法从服务器获取数据,然后检查数据是否返回,我做其他工作。

 private void recyclerViewJobs() {
getPostByPageId();
if (pageDtoList.size() > 0) {
emptyText.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
PagesAdapter adapter = new PagesAdapter(pageDtoList, context);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
emptyText.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}


}

.....

private void getPostByPageId() {

IPageEndPoint pageEndPoint = ServiceGenerator.createService(IPageEndPoint.class);
Call<List<PageDto>> call = pageEndPoint.getPostByPageId(profileId);
call.enqueue(new retrofit2.Callback<List<PageDto>>() {
@Override
public void onResponse(Call<List<PageDto>> call, Response<List<PageDto>> response) {
if (response.isSuccessful()) {
pageDtoList = response.body();
} else {
log.toast("response is not successful for getPostByPageId");
}
}

@Override
public void onFailure(Call<List<PageDto>> call, Throwable t) {
log.toast("getPostByPageId onFailure");
}
});
}

我不知道为什么在 recyclerViewJobs() 方法中,if 条件先工作?

maybe I could not explain my issue very well but my big problem is to know when I want to get some data from REST and then use this data to another REST service, how can I do this job because enqueue in retrofit works asynchronous and do not wait for first method, because of that it is beginning the second method in another thread and because my data was not gotten from first method yet, my program was crashed and I didn't get the answer. That is my problem.....

干杯

最佳答案

那是因为retrofit的enqueue方法不是阻塞方法。这意味着调用它的线程不会等待它完成它的工作。就像@Amir 说的,如果你在调用getPostByPageId() 之后还有其他事情要做,你应该把它们放在retrofit 的回调之后:

    private void recyclerViewJobs() {
getPostByPageId();
}
private void getPostByPageId() {

IPageEndPoint pageEndPoint = ServiceGenerator.createService(IPageEndPoint.class);
Call<List<PageDto>> call = pageEndPoint.getPostByPageId(profileId);
call.enqueue(new retrofit2.Callback<List<PageDto>>() {
@Override
public void onResponse(Call<List<PageDto>> call, Response<List<PageDto>> response) {
if (response.isSuccessful()) {
pageDtoList = response.body();
if (pageDtoList.size() > 0) {
emptyText.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
PagesAdapter adapter = new PagesAdapter(pageDtoList, context);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
emptyText.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
} else {
log.toast("response is not successful for getPostByPageId");
}
}

@Override
public void onFailure(Call<List<PageDto>> call, Throwable t) {
log.toast("getPostByPageId onFailure");
}
});
}

关于android - 为什么异步方法在 Retrofit 2 中调用得更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com