I have fews questions about RoboSpice (advanced usages).
Note: I use RoboSpice with OrmLite
.
My Android application is composed of two main activities: the first one is SplashActivity
(start on launch) and the second is MainActivity
(lauched 5s after the Splash Screen). I perform 2 requests on splash:
SplashActivity
public class SplashActivity extends BaseActivity {
private fooRequest fooRequest;
private barRequest barRequest;
private exampleRequest exampleRequest;
private NewsRequest newsRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
fooRequest = new fooRequest();
barRequest = new barRequest();
...
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Launch MainActivity...
}
}, 5000);
}
@Override
protected void onStart() {
super.onStart();
getSpiceManager().execute(fooRequest, new Integer(0), DurationInMillis.ONE_DAY, new fooRequestListener());
getSpiceManager().execute(barRequest, new Integer(1), DurationInMillis.ONE_DAY, new barRequestListener());
}
public final class fooRequestListener implements RequestListener<Foo> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Log.w("MyApp", "RoboSpice - Foo request failure");
}
@Override
public void onRequestSuccess(final Foo result) {
}
}
public final class barRequestListener implements RequestListener<Bar> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Log.w("MyApp", "RoboSpice - Bar request failure");
}
@Override
public void onRequestSuccess(final Bar result) {
}
}
}
The problem
My application logic is not really reliable: we can not be sure that requests are finished when MainAcitivty is launched. On the MainActivity I query my database with OrmLite for fetch some data and display them. So if request started on SplashActivity are not finished, my View display nothing.
Questions
1) I think that I need to add a listener to my pending requests (if such a request exists). On the RoboSpice Wiki, it said to use spiceManager.addListenerToPendingRequest
. I have not managed to put it out, despite my tests. Maybe I'm doing something wrong? Can you give me code example? Resolved (see below)
2) Currently, the user is still waiting 5 seconds (timer Splash) before arriving at the home screen. How to check if data are in cache? With spiceManager.getDataFromCache()
(it takes into account the expiration duration?).
3) What is the best retry policy to failed requests (a. at the first launched if database is not again created; b. if the database exists but data are expired)?
Edit
Question #1 resolved (I make a mistake in my original code) - #2 and #3 still relevant. Here's what to do (if it can help someone ...):
public class MainActivity extends BaseActivity {
private SpiceManager spiceManager = new SpiceManager(CalendarSpiceService.class);
...
@Override
protected void onStart() {
super.onStart();
spiceManager.start(this);
spiceManager.addListenerIfPending(Foo.class, new Integer(0), new fooRequestListener());
spiceManager.addListenerIfPending(Bar.class, new Integer(2), new newsRequestListener());
spiceManager.getFromCache(Foo.class, new Integer(0), DurationInMillis.ONE_DAY, new fooRequestListener());
spiceManager.getFromCache(Bar.class, new Integer(2), DurationInMillis.ONE_DAY, new newsRequestListener());
}
...
public final class fooRequestListener implements RequestListener<Foo> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Log.w("MyApp", "RoboSpice - Foo request failure");
}
@Override
public void onRequestSuccess(final Foo result) {
String test = result.getResult().iterator().next().getTitle();
Log.w("MyApp", "RoboSpice - Foo request OK ! "+test);
}
}
public final class barRequestListener implements RequestListener<Bar> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Log.w("MyApp", "RoboSpice - Bar request failure");
}
@Override
public void onRequestSuccess(final Bar result) {
Log.w("MyApp", "RoboSpice - Bar request OK ! "+test);
}
}
}
But I don't understand the aim of spiceManager.getFromCache
here...
Thanks!