question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Realm Database not persisting data on next app session

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
farmazon3000commented, Jul 15, 2016
0reactions
olayinkasfcommented, Feb 2, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found