Retrofit2:在 recyclerview 中执行下一个任务之前等待响应

Swapna Yenne

我正在使用 recyclerview 来显示项目列表(图像和文本),我正在从后端获取 recyclerview 列表中显示的项目,我正在使用 retrofit2 进行休息调用,我能够从其余部分获取列表,recyclerview 是完美呈现。

在 recyclerview 中显示项目列表时,我想向项目中存在的图像添加位图图像。在将此位图图像添加到项目中存在的图像之前,我必须进行第二次改造调用(异步)以检查该项目是否需要位图图像,如果响应为真,则只需要添加位图图像。

现在的问题是,当我在改造中进行异步调用(使用入队方法)时,回收站视图不会等待改造的响应,因为我无法在每个项目中存在的图像上绘制位图。

我知道我们可以使用同步调用来解决这个问题,但我不想在性能上妥协。

下面是代码片段供参考。

我从 recyclerview 适配器调用改造方法,它将根据返回值返回布尔值我想在项目图像上绘制位图

改造方法:

HttpRestServiceConsumer.getBaseApiInterface(false)
    .getTestWithURL(imageURL)
    .enqueue(new Callback<TestResponse>() {

        @Override
        public void onResponse(Call<TestResponse> call, Response<TestResponse> response) {

            try {
                if (response.isSuccessful()) {

                    data = response.body().getTrackElements();

                    if (response.body().getTrackElements().size() > 0) 

                          testExist = true;

                   else 

                    testExist=false;


            } catch (Exception e) {

            }

        }

        @Override
        public void onFailure(Call<TestResponse> call, Throwable t) {

        }
铸造厂

我认为您想获得存在性测试以在调用类中注册。一种方法是声明一个接口......

public interface GetTestWithURLCompletion {
    public void getTestWithURLCompletion(boolean success);
}

你的调用类应该采用这个接口......

public class CustomClass implements GetTestWithURLCompletion  {

   public void getTestWithURLCompletion(boolean success) {
         if (success) // do something
   }
}

并且 URL 函数应该接受调用者作为参数:

    public void getTestWithURL(String imageURL, GetVeepWithURLCallback caller);

调用类发送对自身的引用作为对 getTestWithURL 调用的一部分:

    webServiceManager.getTestWithURL(imageURL, this);

然后getTestWithURL可以回调调用调用类中的接口:

 caller.getTestWithURLCompletion(testExist);

完整示例如下所示:

//interface
public interface GetTestWithURLCompletion {
    public void getTestWithURLCompletion(boolean success);
}

//api access class
public class ApiManager {

    //getTestWithURL
    public void getTestWithURL(String imageURL, GetVeepWithURLCallback caller) {

    HttpRestServiceConsumer.getBaseApiInterface(false)
    .getTestWithURL(imageURL)
    .enqueue(new Callback<TestResponse>() {

        @Override
        public void onResponse(Call<TestResponse> call, Response<TestResponse> response) {

            try {
                if (response.isSuccessful()) {

                    data = response.body().getTrackElements();

                    if (response.body().getTrackElements().size() > 0) {
                      caller.getTestWithURLCallback(true);
                    } else {
                      caller. getTestWithURLCallback(false);
                   }
            } catch (Exception e) {

            }
        }
        @Override
        public void onFailure(Call<TestResponse> call, Throwable t) {
        }
   }
}


//calling class
public class CustomClass implements GetTestWithURLCompletion  {

   //calling function
   public void someFunction {
       apiManager.getTestWithURL(imageURL, this)
  }

   //callback function
   public void getTestWithURLCompletion(boolean success) {
         if (success) // do something
   }
}

Java 专家(我不是其中之一)可能能够通过使用匿名函数或 lambda 表达式的示例来增强此答案。将匿名函数传递给 getTestWithUrl 将无需提供单独的回调函数,并且可以使此模式更易于移植。它可能看起来像...

apiManager.getTestWithUURL(imageURL,(boolean success) -> {
             if (success) // do something
})

(这个语法肯定是错误的——当作伪代码处理!)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章