I used the following methods to solve this problem. In addition, the code below will solve the issue when your activity A is loading and your user decides to navigate to activity B in the meantime while robospice is loading in the background. When the user navigates back to activity A, your information will be ready for consumption (Issue 2).
STEP 1: Add the following code to add a PendingRequestListener
in the onStart
method. This will allow you to solve issue 2 as the PendingRequestListener
will listen to all pendingRequest in robospice and deal with them appropriately.
@Override
public void onStart() {
super.onStart();
FragmentPostActivity.spiceManagerPlaces.addListenerIfPending(Post.class, "POSTS", pendingRequestListener);
}
STEP 2: Create your PendingRequestListener
.
PendingRequestListener pendingRequestListener = new PendingRequestListener() {
@Override
public void onRequestNotFound() {
//This is when no request is found at all - this method will be triggered. Notice that I have
//placed this method within the onStart method which means that when your app onStarts - when you
//click on the app icon on your phone, this method will actually be triggered so therefore it will first
//get the data from cache by using the cacheKey "POSTS", then it will trigger a separate spiceRequest for network
//operations to fetch the data from the server. Notice that the second spiceRequest does not have a cacheKey.
//The reason is that we are not going to return back the results of the network operations - we will return
//the results of our cache that we will manually create at a later stage
PostSpiceRequest postSpiceRequest = new PostSpiceRequest(getActivity(), postid);
FragmentPostActivity.spiceManagerPlaces.getFromCache(EmbedPost.class, "POSTS", DurationInMillis.ONE_WEEK, new PostListener());
FragmentPostActivity.spiceManagerPlaces.execute(PostSpiceRequest,new PostListener());
}
@Override
public void onRequestFailure(SpiceException spiceException) {
//This is if your request failed when you navigate back to Activity A from Activity B
Log.e("PendingRequestRS", "request failed for pending requests");
}
@Override
public void onRequestSuccess(Object o) {
//This is if your request succeed when you navigate back to Activity A from Activity B
Log.e("PendingRequestRS", "pending request successful");
FragmentPostActivity.spiceManagerPlaces.getFromCache(Post.class, "POSTS", DurationInMillis.ONE_WEEK, new PostListener());
}
};
STEP 3: Add a manual method to cache your data so that it can be retrieved when your app starts up again. The method below counts up to 20 posts and then puts them into the cache. If it is less than 20 posts, it will cache up to that amount.
@Override
public void onPause() {
super.onPause();
//If you are using a spiceManager from your activity class, you will needs to put this code into onPause and not in onStop.
//Inside your activity, you would have stopped the spicemanager in onStop, which means that your cache would not be stored
//at all.
//If you are using a spiceManager in your fragment class, you are free to put this code within your onStop method but make
//sure that it is place in front of the spiceManager.onStop() so that the method will execute before it spicemanager is stopped
LinkedList<Post> cachedPosts = new LinkedList<>();
if (posts.size() > 20) {
for (int i = 0; i <= 20; i++) {
cachedPosts.add(posts.get(i));
}
} else {
for (int i = 0; i < posts.size(); i++) {
cachedPosts.add(posts.get(i));
}
}
PostActivity.spiceManager.removeDataFromCache(Post.class, "POSTS");
new Thread(new Runnable() {
@Override
public void run() {
try {
FragmentPostActivity.spiceManagerPlaces.putDataInCache("POSTS", cachedPosts);
} catch (CacheSavingException e) {
e.printStackTrace();
} catch (CacheCreationException e) {
e.printStackTrace();
}
}
}).start();
}
Hopefully I have shed some light on this topic of using Robospice as there is very little documentation as to how this is done.