【问题标题】:Robospice - addListenerIfPending - how do I know if request WAS found?Robospice - addListenerIfPending - 我如何知道是否找到了请求?
【发布时间】:2018-01-22 12:55:04
【问题描述】:

我正在尝试使用 robospice 实现一个非常简单的案例:登录屏幕。单击登录按钮时,会显示加载动画并使用SpiceManager.execute(..) 发出请求(当然没有缓存。由于它是登录请求,因此我每次都需要对服务器进行实际调用)。

所以,为了处理屏幕旋转等,在Activity.onStart() 中我应该使用SpiceManager.addListenerIfPending(..),如果请求实际上是待处理的,我应该在不做任何实际请求的情况下显示加载动画。

问题是:PendingRequestListener 没有onRequestExists() 方法。它只有onRequestNotFound()

一种可能(但不令人满意)的解决方案是始终绘制加载屏幕,然后调用addListenerIfPending(..),然后删除onRequestNotFound() 中的加载动画。如果没有真正的请求,这会创建“闪烁”的加载屏幕,这看起来非常糟糕。实现所需行为的正确方法是什么?

【问题讨论】:

  • 使用缓存。见stackoverflow.com/a/27136012/2440169,类似问题。
  • 看起来像一个非常肮脏的黑客,这也需要编写正确的缓存实现......我想我会尝试,但实际上我希望从 Robospice 找到足够的 API。
  • 如果您使用来自 Robospice 扩展(Spring、Google HTTP 客户端、Retrofit)的预定义服务,则缓存已经实现。

标签: android robospice


【解决方案1】:

好的,以防谷歌在这里引导任何人。 适当的解决方案: 实际上,该功能已经在库中。当发现待处理的请求时,有一个方法会被调用,它只是......什么都不做。

你需要:

  1. 创建您自己的扩展PendingRequestListener的接口:

    public interface MyPendingRequestListener<T> extends PendingRequestListener<T> {
        void onRequestAggregated();
    }
    

    注意:通过 Robospice 进行请求时,您需要使用实现此接口的请求侦听器。

  2. 创建您自己的实现RequestListenerNotifier 的类。我通过复制粘贴 DefaultRequestListenerNotifier 来做到这一点(我无法扩展它,因为方法 post(..) 是私有的),因为它包含其他情况的 porper 通知实现。例如,MyRequestListenerNotifier

  3. 覆盖方法notifyListenersOfRequestAggregated

    @Override
    public <T> void notifyListenersOfRequestAggregated(final CachedSpiceRequest<T> request, Set<RequestListener<?>> listeners) {
        post(new AggregatedRunnable(listeners), request.getRequestCacheKey());
    }
    
    private static class AggregatedRunnable implements Runnable {
        private final Set<RequestListener<?>> listeners;
    
        public AggregatedRunnable(final Set<RequestListener<?>> listeners) {
            this.listeners = listeners;
        }
    
        @Override
        public void run() {
    
            if (listeners == null) {
                return;
            }
    
            Ln.v("Notifying " + listeners.size() + " listeners of request aggregated");
            synchronized (listeners) {
                for (final RequestListener<?> listener : listeners) {
                    if (listener != null && listener instanceof MyPendingRequestListener) {
                        Ln.v("Notifying %s", listener.getClass().getSimpleName());
                        ((MyPendingRequestListener<?>) listener).onRequestAggregated();
                    }
                }
            }
        }
    }
    
  4. 扩展 SpiceService 并覆盖以下方法(您将需要使用此自定义服务而不是标准 robospice 服务):

    protected RequestListenerNotifier createRequestRequestListenerNotifier() {
        return new MyRequestListenerNotifier();
    }
    

就是这样。现在,当您使用addListenerIfPending 时,当找到待处理的请求时将调用onRequestAggregated()

重要提示:这仅在您在请求时使用缓存键时有效。例如,您可以使用空字符串(但 NOT 为空)作为缓存键。

例子:

//doing request for the first time
spiceManager.execute(request, "", DurationInMillis.ALWAYS_EXPIRED, requestListener);
//...     
//checking if request is pending e.g. when activity is restarted
spiceManager.addListenerIfPending(responseClass, "", requestListener);

【讨论】:

    猜你喜欢
    • 2017-06-16
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多