Using background thread to generate reports


Hi, I am currently using a background thread to generate reports and appear to have an instance where the background thread is not seeing the data saved on the UI thread.

In this scenario the user has made a bunch of changes and then soon after making the changes they run a print job which then opens the realm from a background thread, reads the data and generates the report.

Why is the background thread not seeing the recently committee changes? In this case the data seems to have been updated midway through generation of the report.

I would expect that if the app opens the realm on a background thread after the users has update the data the background thread would see these changes.

Is there some way to ‘commit’ the changes from the main thread such that any subsequent access from a background thread will see these changes ?

Does Realm do some sort of lazy write even though CommitWrite() has been called


You can force-update the realm on the background thread, with a realm.refresh() call.

There is some time while the new data will be saved to the realm, and all threads synced - especially if you are using a lot of power.


So if I have a user doing updates on the main thread how can I be sure all the changes have been committed to disk and will be available in the background thread. The user will only start the report (background thread) after they have completed updates and the app will have done a CommitWrite() on the main thread.

How will the app know when the data changes on the main thread are committed and will be available to the background thread ?

Bear in mind that the background thread won’t open the realm until the user attempts to generate the report - but I need to make sure that when they do run the report the updates made on the UI thread are committed and will be available to the background thread.

Sounds like refresh() is more useful for force updating a realm that was opened before the UI updates were made.


For clarification:

Are you saying the user makes a “bunch of changes”, then 10 minutes later they decide to run a report and the report data does not contain the changes they made?


They make a bunch of changes, maybe 10 or 20 new or changed entities, in the ui and immediately after they generate a report - so a few seconds, or as long as it takes for them to switch to the report screen and click on the generate report button.


I dunno. That doesn’t sound right. Our apps have a similar setup where the user can make changes and then run a report and we never run into an issue where the data is stale. From the docs on Threading

When you initially open a Realm on a thread, its state will be based off the most recent successful write commit, and it will remain on that version until refreshed

Do you have autorefresh set to NO? Are you using Realm.refresh() to advance the transaction to the most recent state?


I wasn’t using Realm.refresh() because the reporting thread only opens the realm after the UI commit has been done. In any event I have added a call to realm.refresh() immediately before running the queries to extract the report data and so far no more reports of incomplete data.

I could not reproduce the error myself so maybe a case of user doing something that only users can do…