【问题标题】:Retrofit2: wait for response before executing next task in recyclerviewRetrofit2:在recyclerview中执行下一个任务之前等待响应
【发布时间】:2017-11-24 14:56:36
【问题描述】:

我正在使用 recyclerview 显示项目列表(图像和文本),我正在从后端获取 recyclerview 列表中显示的项目,我正在使用 retrofit2 进行休息调用,并且我能够从其余部分获取列表并且 recyclerview 渲染得非常好。

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

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

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

下面是sn-ps的代码供参考。

我从 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) {

        }

【问题讨论】:

    标签: android-recyclerview retrofit


    【解决方案1】:

    我认为您希望在调用类中注册存在测试。 一个方法是声明一个接口...

    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
    })
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      相关资源
      最近更新 更多