Hi,
I have few issues:
when should is set realm.setAutoRefresh(true) ?
realm.setAutoRefresh() is automatically true for any thread that is associated with a Looper (for example the UI thread)
Better question is "when would you set realm.setAutoRefresh(false)", and I'm not sure. 馃槃 probably never
what is the difference between realm.refresh() and realm.setAutoRefresh() ?
realm.refresh() immediately forces Realm to update its version and evaluate all active RealmResults synchronously on the current thread.
realm.setAutoRefresh() just means it'll receive notifications from Realm on looper thread to handle Realm notifications.
if autoRefresh set to true, will realm.refresh() called every time i get realm instance through realm.getDefaultInstance() ?
No
when to use realm.refresh()
On non-looping background thread outside of transaction where you execute scheduled task and "do something depending on some condition in Realm"
@beeender I accidentally removed the waiting for user by adding a " into my answer, sorry
@Zhuinden Thank you for detailed explanation.
i am saving data in thread A and accessing from thread B.
some times, that data is not reflecting on realm instance of other threads (B).
So, every time i am using realm.refresh() on realm instance of Thread B before making a realm query. Is this good ? any downsides ?
no, it is not good.
You should use notifications instead. https://realm.io/docs/java/latest/#notifications
@srinivas3120 depends on what is thread B
(UI thread or another non-looping background thread)
But if it is non-looping background thread then if opening new Realm returns an old version then that can mean you are not closing your Realm instances properly which can create problems including large database file size
Thread B is a UI thread.
depends on what is thread B (UI thread or another non-looping background thread)
@Zhuinden what will happen if its another non-looping background thread ?
The problem is, i am not using realm live objects in my whole app :(.
once we get the data from realm, we make a java object and close the realm instance.
i am not using realm live objects
@srinivas3120 then create the unmanaged copies on the thread that executed the transaction, and keep your reads off the ui thread
(but realm.refresh() would force it to be up to date too, it is not the intended usage though)
save data in thread A and access it from thread B. and i dont use live realm objects.
some times data is not reflecting on realm instance of thread B.
because of which i started using realm.refresh() on realm instance of Thread B before reading realm data.
@srinivas3120 then create the unmanaged copies on the thread that executed the transaction, and keep your reads off the ui thread
@Zhuinden I have to read from UI Thread B
what are the down sides of it.
realm.refresh() immediately forces Realm to update its version and evaluate all active RealmResults synchronously on the current thread.
is there any other way of doing it ?
using RealmChangeListener to be notified of change
....or maybe also using realm.executeTransactionAsync(Realm.Transaction.OnSuccess) from UI thread.
You're probably just looking for realm.refresh() tho
I dont have any active RealmResults as i am not using live objects.
forces Realm to update its version
does this mean, realm gets reinitialised ?
@srinivas3120 it means that instead of pointing at an old version, it starts pointing to new version
Anyways, considering you don't use RealmChangeListener at all, probably realm.refresh() is the right solution for you.
Although life is so much easier when you're using a reactive data layer. I don't know why people often don't. Not even the Google samples that use Flowable integration with Room do that because they have this NetworkBoundResource that is completely unnecessary.
Just my thoughts, though.
Yeah, using reactive data layer is right way to update the objects instead of re querying them.
Actually we migrated to Realm from ActiveAndroid.
If you see any other databases(ex: sqlite), once transaction is completed it is persisted through out app/device.
but incase of realm, it is getting persisted on the current thread. it takes little time to reflect on other threads. in the mean time you try to get that data from other threads, you don't get that data! this becomes a issue if you don't use live objects(reactive data layer). For us, converting to live objects is quite difficult and time taking.
@srinivas3120 you're lucky that realm.refresh() was re-added in 3.2 馃槃
Hey @srinivas3120
It looks to me as if we've answered your question. I'm going to close this issue. Please feel absolutely free to open a new one if you have more questions. Better yet, ask on StackOverflow
-blake
Most helpful comment
realm.setAutoRefresh()is automaticallytruefor any thread that is associated with a Looper (for example the UI thread)Better question is "when would you set
realm.setAutoRefresh(false)", and I'm not sure. 馃槃 probably neverrealm.refresh()immediately forces Realm to update its version and evaluate all active RealmResults synchronously on the current thread.realm.setAutoRefresh()just means it'll receive notifications from Realm on looper thread to handle Realm notifications.No
On non-looping background thread outside of transaction where you execute scheduled task and "do something depending on some condition in Realm"