. , , A , B , . A, ( 2).
1: , PendingRequestListener onStart. 2, PendingRequestListener pendingRequest robospice .
@Override
public void onStart() {
super.onStart();
FragmentPostActivity.spiceManagerPlaces.addListenerIfPending(Post.class, "POSTS", pendingRequestListener);
}
2: PendingRequestListener.
PendingRequestListener pendingRequestListener = new PendingRequestListener() {
@Override
public void onRequestNotFound() {
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) {
Log.e("PendingRequestRS", "request failed for pending requests");
}
@Override
public void onRequestSuccess(Object o) {
Log.e("PendingRequestRS", "pending request successful");
FragmentPostActivity.spiceManagerPlaces.getFromCache(Post.class, "POSTS", DurationInMillis.ONE_WEEK, new PostListener());
}
};
STEP 3: Add a manual data caching method so that it can be restored when your application starts up again. The method below counts up to 20 messages, and then puts them in the cache. If it is less than 20 messages, it will cache up to this amount.
@Override
public void onPause() {
super.onPause();
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();
}
I hope I shed some light on the subject of using Robospice, since there is very little documentation on how to do this.
Simon source
share