Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

Advanced RoboSpice Usages and FAQ

stephanenicolas edited this page Dec 15, 2012 · 10 revisions

This page is dedicated to explain advanced RoboSpice usages and answer frequently asked questions

How to cancel a request ?

You can cancel a pending request by either

  1. using the request object itself and cancelling it
  2. cancelling a request using its cache key and result type via the spice manager. This is preferred as the cache key can be saved in the onSaveInstanceState bundle.

The first mechanism is hard to put it place on Android as you can loose a request object very easily (during a rotation for instance) but can still be considered in some simple cases (where rotation is blocked for instance).

How to add a listener to a pending requests ?

The SpiceManager will let you add a listener to a request if such a request exists by using :

  1. spiceManager.addListenerToPendingRequest

This will not execute any request, just add a listener. If no such request exists, it won't do anything.

How to get request's results from cache without triggering any network processing ?

Use spiceManager.getDataFromCache() using the result type and the cache key as a compound key to identify request's result in the cache.

How can I access HTTP headers using RoboSpice ?

The core of RoboSpice is dedicated to networking but no to HTTP. It is protocole-agnostic and that provides the ability to use RoboSpice for HTTP/SPDY/FTP/etc. networking.

If you decide to use a more specific technology in conjunction with RoboSpice for handling an HTTP scenario, such as Spring Android, Google Http Java Client, etc. they will provide you, or not, with the headers of the response.

For instance [http://static.springsource.org/spring-android/docs/1.0.x/reference/html/rest-template.html](Spring Android RESTTemplate) does have "entity*" versions of its methods that can give you back the headers. But again, this is not the responsibility of RoboSpice.

Can we get HTTP headers to determine cache duration for a given request's result ?

When you execute a request using the SpiceManager, you can provide a duration of expiry. This duration indicates whether or not to use any data in the cache, if there are some. It doesn't indicate any expiry duration of the data that the current request will fetch from the network.

Report to question above to get the HTTP headers and use the returned value for data expiration to give an expiry duration to your next request.

Can I use RoboSpice for a non-network related task like a long-running computation ?

Yes, you can, and that should be the best option around. RoboSpice has been designed for networking requests but we acknowledge that it addresses a more general problem : syncing long running background tasks and activities.

To perform such a task using RoboSpice there are 2 steps to follow :

  • build you request as usual but don't use any networking in the method "loadDataFromNetwork"
  • provide your Spice Service with a custom NetworkStateChecker that will always declare the network to be available. This way, RoboSpice will always execute your requests (usually, requests are simply discarded if network is not on).