Realm Database not persisting data on next app session
See original GitHub issueI am writing to a Realm database (with beginTransaction) and then soon after commit, I can halt the process in debugger and run a query and find all the written data being queried.
Then I restart the debugger (Android Studio), and I run the same query (and I see that Realm realm object has the same file name), but the query this time is unable to find any data
// inside the Application class
if (appFirstTime == false){
Realm realm3 = Realm.getInstance(getApplicationContext());
RealmResults<FeedDBTableRow> query3 = realm3.where(FeedDBTableRow.class).findAll();
// here, in the second run of the app, after data is written in the first time run,
// i can't see any rows, query size = 0 although it should have 10 rows
}
else{
// get data and write on the first time of the app
ArrayList<Feed> newsFeedList = response.getResults();
Realm realm = Realm.getInstance(getApplicationContext());
for (Feed f : newsFeedList) {
realm.beginTransaction();
FeedDBTableRow newRow = realm.createObject(FeedDBTableRow.class);
newRow.setId(f.getId());
newRow.setText(f.getText());
newRow.setTime_created(f.getTime_created());
newRow.setTime_modified(f.getTime_modified());
newRow.setComments_count(f.getComments_count());
newRow.setLikes_count(f.getLikes_count());
newRow.setFeed_type(f.getFeed_type());
newRow.setObj_id(f.getObj_id());
newRow.setImage(f.getImage());
newRow.setUser_name(f.getUser_name());
newRow.setUser_earthmile_points(f.getUser_earthmile_points());
newRow.setLiked(f.isLiked());
newRow.setCommented(f.isCommented());
newRow.setIs_private(f.is_private());
newRow.setUrl(f.getUrl());
newRow.setFeed_creator_id(f.getFeed_creator_id());
realm.commitTransaction();
}
Realm realm2 = Realm.getInstance(getApplicationContext());
RealmResults<FeedDBTableRow> query2 = realm2.where(FeedDBTableRow.class).findAll();
// here I can see all the 10 rows in query object in database, when i query soon after writing in the same app session
// set first time to false in preferences so that next time database is accessed
appFirstTime = false
}
Realm class
public class FeedDBTableRow extends RealmObject {
private int id;
private String text;
private Date time_created;
private Date time_modified;
private int comments_count;
private int likes_count;
private String feed_type;
private int obj_id;
private String image;
private String user_name;
private String user_earthmile_points;
private boolean liked;
private boolean commented;
private boolean is_private;
private String url;
private int feed_creator_id;
}
Issue Analytics
- State:
- Created 9 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Realm does not seem to persist data between session or app ...
One of the very first operation the app is performing just after starting is to access a local database and look for the...
Read more >Realm Flexible Sync not working properly in Swift - MongoDB
Hi there, we try to implement the “Restricted News Feed” example in Swift from the Flexible Sync Permissions Guide.
Read more >Integrating Realm Database in an Android Application - Auth0
Learn how to integrate the Realm mobile database in an Android application.
Read more >Android Databases | 6 Important Considerations - Realm.io
A database for Android is a form of persistent data storage intended for ... of application while meeting the most demanding requirements for...
Read more >Realm Xamarin 2.0.0 - AWS
A Realm is an instance of a Realm Database container. Realms can be local or synchronized. A synchronized Realm uses the Realm Object...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This issues (and similar like this http://stackoverflow.com/questions/27041093/realm-java-data-lost-when-restarting-application) are because you Realm are making examples with
in Application.#onCreate() like so https://github.com/realm/realm-java/blob/61d14cbcb0419c1962f044500e34d92ac23112a4/examples/threadExample/src/main/java/io/realm/examples/threads/MyApplication.java.
Fix your examples.
I’m having the same problem, I persist in the background send a broadcast to the opened activity and the activity can’t find the persisted data.