/** * Execute a request, without using cache. No result from cache will be * returned. The method {@link SpiceRequest#loadDataFromNetwork()} will * always be invoked. The result will not be stored in cache. * @param request * the request to execute. * @param requestListener * the listener to notify when the request will finish. */ public <T> void execute(final SpiceRequest<T> request, final RequestListener<T> requestListener) { final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, null, DurationInMillis.ALWAYS_RETURNED); execute(cachedSpiceRequest, requestListener); }
/** * Execute a request. Before invoking the method * {@link SpiceRequest#loadDataFromNetwork()}, the cache will be checked : * if a result has been cached with the cache key <i>requestCacheKey</i>, * RoboSpice will consider the parameter <i>cacheExpiryDuration</i> to * determine whether the result in the cache is expired or not. If it is not * expired, then listeners will receive the data in cache. Otherwise, the * method {@link SpiceRequest#loadDataFromNetwork()} will be invoked and the * result will be stored in cache using the cache key * <i>requestCacheKey</i>. * @param request * the request to execute * @param requestCacheKey * the key used to store and retrieve the result of the request * in the cache * @param cacheExpiryDuration * duration in milliseconds after which the content of the cache * will be considered to be expired. * {@link DurationInMillis#ALWAYS_RETURNED} means data in cache * is always returned if it exists. * {@link DurationInMillis#ALWAYS_EXPIRED} means data in cache is * never returned.(see {@link DurationInMillis}) * @param requestListener * the listener to notify when the request will finish */ public <T> void execute(final SpiceRequest<T> request, final Object requestCacheKey, final long cacheExpiryDuration, final RequestListener<T> requestListener) { final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, cacheExpiryDuration); execute(cachedSpiceRequest, requestListener); }
/** * @See #addListenerIfPending(Class, Object, PendingRequestListener) */ @Deprecated public <T> void addListenerIfPending(final Class<T> clazz, final Object requestCacheKey, final RequestListener<T> requestListener) { final SpiceRequest<T> request = new SpiceRequest<T>(clazz) { @Override public T loadDataFromNetwork() throws Exception { return null; } }; final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, DurationInMillis.ALWAYS_EXPIRED); cachedSpiceRequest.setProcessable(false); execute(cachedSpiceRequest, requestListener); }
@Override protected void onStart() { super.onStart(); getJsonContentManager().execute(loremRequest, "lorem.txt", DurationInMillis.ONE_DAY, new LoremRequestListener()); getJsonContentManager().execute(weatherRequest, "75000.json", DurationInMillis.ONE_DAY, new WeatherRequestJsonListener()); getJsonContentManager().execute(weatherRequestXml, "75000.xml", DurationInMillis.ONE_DAY, new WeatherRequestXmlListener()); getJsonContentManager().execute(imageRequest, "logo", DurationInMillis.ONE_DAY, new ImageRequestListener()); }
@Override public void onStart() { super.onStart(); getSpiceManager().execute( new ResetSpiceRequest(getContext().getApplicationContext()), TAG, DurationInMillis.ALWAYS_EXPIRED, this ); }
/** * Adds some data to the cache, asynchronously. * @param clazz * a super class or the class of data. * @param requestCacheKey * the request cache key that data will be stored in. * @param data * the data to store. Maybe null if supported by underlying * ObjectPersister. * @param listener * a listener that will be notified of this request's success or * failure. May be null. */ public <U, T extends U> void putInCache(final Class<U> clazz, final Object requestCacheKey, final T data, RequestListener<U> listener) { @SuppressWarnings({ "unchecked", "rawtypes" }) final SpiceRequest<U> spiceRequest = new SpiceRequest(clazz) { @Override public U loadDataFromNetwork() throws Exception { return data; } }; CachedSpiceRequest<U> cachedSpiceRequest = new CachedSpiceRequest<U>(spiceRequest, requestCacheKey, DurationInMillis.ALWAYS_EXPIRED); cachedSpiceRequest.setOffline(true); execute(cachedSpiceRequest, listener); }
/** * Gets data from cache, expired or not, and executes a request normaly. * Before invoking the method {@link SpiceRequest#loadDataFromNetwork()}, * the cache will be checked : if a result has been cached with the cache * key <i>requestCacheKey</i>, RoboSpice will consider the parameter * <i>cacheExpiryDuration</i> to determine whether the result in the cache * is expired or not. If it is not expired, then listeners will receive the * data in cache only. If the result is absent or expired, then * {@link SpiceRequest#loadDataFromNetwork()} will be invoked and the result * will be stored in cache using the cache key <i>requestCacheKey</i>. * @param request * the request to execute * @param requestCacheKey * the key used to store and retrieve the result of the request * in the cache * @param cacheExpiryDuration * duration in milliseconds after which the content of the cache * will be considered to be expired. * {@link DurationInMillis#ALWAYS_RETURNED} means data in cache * is always returned if it exists. * {@link DurationInMillis#ALWAYS_EXPIRED} doesn't make much * sense here. * @param requestListener * the listener to notify when the request will finish */ public <T> void getFromCacheAndLoadFromNetworkIfExpired(final SpiceRequest<T> request, final Object requestCacheKey, final long cacheExpiryDuration, final RequestListener<T> requestListener) { final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, cacheExpiryDuration); cachedSpiceRequest.setAcceptingDirtyCache(true); execute(cachedSpiceRequest, requestListener); }
final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, cacheExpiryDuration); cachedSpiceRequest.setOffline(true); execute(cachedSpiceRequest, requestListener);
/** * Cancel a pending request if it exists. If no such request exists, this * method does nothing. If a request identified by clazz and requestCacheKey * exists, it will be cancelled and its associated listeners will get * notified. This method is asynchronous. * @param clazz * the class of the result of the pending request to look for. * @param requestCacheKey * the cache key associated to the request's results. */ public <T> void cancel(final Class<T> clazz, final Object requestCacheKey) { final SpiceRequest<T> request = new SpiceRequest<T>(clazz) { @Override public T loadDataFromNetwork() throws Exception { return null; } }; final CachedSpiceRequest<T> cachedSpiceRequest = new CachedSpiceRequest<T>(request, requestCacheKey, DurationInMillis.ALWAYS_EXPIRED); cachedSpiceRequest.setProcessable(false); cachedSpiceRequest.setOffline(true); cachedSpiceRequest.cancel(); execute(cachedSpiceRequest, null); }
@Override public void onStart() { super.onStart(); if(!hasStartedRequest) { getSpiceManager().execute(getRequest(), getCacheKey(), DurationInMillis.ALWAYS_EXPIRED, this); hasStartedRequest = true; } else { getSpiceManager().addListenerIfPending(getRequest().getResultType(), getCacheKey(), this); } }