Robospice - addListenerIfPending - 我怎么知道,如果reqest被发现? [英] Robospice - addListenerIfPending - how do I know if reqest WAS found?
问题描述
我想实现使用robospice一个非常简单的例子:一个登录界面。当点击登录按钮,加载动画显示,并要求用做 SpiceManager.execute(..)
(没有缓存,当然,因为它是一个登录请求,我需要做实际调用到服务器的每一次)。
I am trying to implement a very simple case using robospice: a login screen. When login button is clicked, loading animation is shown and request is made using SpiceManager.execute(..)
(without caching, of course. Since it's a login request, I need to do actual call to the server every time).
因此,要处理的屏幕旋转等,在 Activity.onStart()
我应该使用 SpiceManager.addListenerIfPending(..)
,如果要求实际上是悬而未决,我应该显示加载动画没有做任何真正的要求。
So, to handle screen rotations, etc., in Activity.onStart()
I should use SpiceManager.addListenerIfPending(..)
, and if request is actually pending, I should show loading animation without doing any real request.
问题是: PendingRequestListener
没有 onRequestExists()
方法。它只有 onRequestNotFound()
。
The problem is: PendingRequestListener
does not have onRequestExists()
method. It only has onRequestNotFound()
.
一个可能的(但不理想)的解决方案,就是总是吸引加载屏幕,然后调用 addListenerIfPending(..)
,然后删除加载动画在 onRequestNotFound()
。如果没有真正的请求,这看起来非常糟糕这就产生闪烁载入画面。什么是实现期望的行为的正确方法?
One possible (but unsatisfactory) solution, is to draw loading screen always, then call addListenerIfPending(..)
, and then remove loading animation in onRequestNotFound()
. This creates "flickering" loading screen if there was no real request, which looks really bad. What is the proper way of implementing desired behavior?
推荐答案
好吧,如果谷歌在这里领导任何人。
妥善解决:
在现实中,这种功能是已在库中。有一个在等待处理的请求,发现其被调用的方法,它少了点什么都没有。
Ok, in case google leads anyone here. Proper solution: in reality, this functionality is already in the library. There is a method which gets called when pending request WAS found, it just does... nothing.
您需要:
-
创建扩展自己的界面
PendingRequestListener
:
public interface MyPendingRequestListener<T> extends PendingRequestListener<T> {
void onRequestAggregated();
}
请注意:您将需要使用的请求listeneres做通过Robospice请求时实现了这个接口
Note: you will need to use request listeneres implementing this interface when doing requests through Robospice.
创建自己的类实现 RequestListenerNotifier
。我通过复制粘贴 DefaultRequestListenerNotifier
(我是不是能延长它,因为方法后(..)
是这样做私营),因为它包含了其他案件porper通知执行。例如, MyRequestListenerNotifier
。
Create your own class implementing RequestListenerNotifier
. I did this by copy-pasting DefaultRequestListenerNotifier
(I was not able to extend it because method post(..)
is private) because it contains porper notification implementation for other cases. For example, MyRequestListenerNotifier
.
覆盖方法 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();
}
}
}
}
}
扩展SpiceService并覆盖下面的方法(你将需要使用此定制的服务,而不是标准robospice服务):
Extend SpiceService and override the following method (you will need to use this customized service instead of standard robospice service):
protected RequestListenerNotifier createRequestRequestListenerNotifier() {
return new MyRequestListenerNotifier();
}
就是这样。现在,当你使用 addListenerIfPending
, onRequestAggregated()
时挂起的请求被发现就会被调用。
That's it. Now, when you use addListenerIfPending
, onRequestAggregated()
will be called when pending request IS found.
重要提示:这将只有当你在做请求时使用缓存的关键工作。例如,您可以使用空字符串(但不会 NULL)作为缓存键。
IMPORTANT NOTE: This will only work when you use Cache key when doing request. For example, you can use empty string (but NOT null) as cache key.
例如:
//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);
这篇关于Robospice - addListenerIfPending - 我怎么知道,如果reqest被发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!