How do I persist the data that has been parsed from the server using Retrofit library. So that users can view it when there is no internet connection.
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.post_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<ArrayList<Post>> call = apiService.getAllPosts();
call.enqueue(new Callback<ArrayList<Post>>() {
@Override
public void onResponse(Call<ArrayList<Post>> call, Response<ArrayList<Post>> response) {
int statusCode = response.code();
List<Post> posts = response.body();
recyclerView.setAdapter(new PostListAdapter(posts, R.layout.activity_main, getApplicationContext()));
}
@Override
public void onFailure(Call<ArrayList<Post>> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
I want to save response, persist and show data into RecyclerView. Help to save using ORM will be appreciated, I try to use Sugar ORM and ActiveAndroid but I can not succeed on that :(
When I extend SugarRecord or Model App terminate without any error on log cat
After copious research, I succeed to persist the response parsed using Retrofit. I have used Realm to persist the data for offline use. I will describe the methods I had implemented which may help other to encounter this type of problems.
1. Add Realm Dependencies to Gradle
App-level Gradle file
2. Extends RealmObject
3. Create Realm Configuration if not exist
4. Save parsed Data
5. Query and Inflate data saved by Realm
Following the above methods help me to solve my problem and I hope this will help to solve your problem too. If you get any problem implementing the solution, you can ask me on comment