I have an android phone app sending the whole realm file through data item api to the wear app. There is a service on wear app to replace the local realm file with the latest one sent from handheld. The problem is that the list view on wear app won't get updated after received new realm file on run time. It does show the new data after restarted the wear app. Is there any way to tell realm that the realm file is changed manually on run time?
2 Answers
This is a very interesting use case!
But I am afraid to say what you are doing now may cause unexpected result.
When the wear app open the Realm (Let's say Realm-file-A
), Realm will open a file descriptor and use it until it gets closed. It is possible to use file API/system command to delete/override the Realm file even when the file descriptor is opened. Now, when the file is deleted and the Realm is still opened, the app will still work without any crash, and this is guaranteed by the OS. See this question to understand this behaviour.
So in your case, since the Realm in wear app is not closed (I guess), after the Realm file is replaced by another file from handheld (Let's say Realm-file-B
), all of the already open Realm instances in the wear app will still read from/write to the previous one -- Realm-file-A
. And newly created Realm instance will work on Realm-file-B
. This scenario is NOT handled by the Realm, and you might meet strange inconsistency problems with this.
So the suggestion is, Before sending the whole Realm file to the wear app, ensure all of the Realm instances in the wear app are closed. After sending, open the Realm instance again, and manually refresh the list view.
However, Realm should support this better. An issues is created for tracking this https://github.com/realm/realm-java/issues/2007 .
-
1hmm, this makes sense. I haven't found a quick way to make the sync task more simpler. I was trying to serialize the realm object and send them individually each time whenever change happens. Do you think if it's the right way to do the synchronization? – Enthalpy_Yan Dec 28 '15 at 22:16
-
Do you ever need to change the data on the wearable side? We do support notification between iOS and watchOS, but it seems the architecture of Android wearable is totally different and we don't have a reliable way to make the Realm file on sync for both side. It might be a headache if both sides want to change the Realm data. – beeender Dec 29 '15 at 05:11
-
yea, I only give the ability to insert new data on wearable side. The realm data on wearable only changes after received notification(sent by data item api) from handheld. The user cannot modify realm directly without notification on the watch. I just refactored to send new changes in json. It worked. – Enthalpy_Yan Dec 29 '15 at 12:07
This is a quite old question, but it is relevant I think.
The point is, as described by @beeender that as long as the file descriptor is open, the old realm is used. You can even delete the old realm file, the inmemory representation is still there and will still be used. Only new instances have the new file backed.
As realm is not fixing this, one have to go an own way. I see two possibilities:
- Use reactiveX (or a similar approach) to make your app reactive. Every place where you read data should be bound to one signal that is emitting as soon as you replaced the database, causing them to close the old, and open a new realm instance.
- Go the plain old way by having some listener interface and some instance informing all those listeners that a new realm file is there, causing them to close all opened realm instances and open new ones.
I would prefer some reactive way as you can also handle realm objects reactive and you would have nice clean signals updating you UI.
Hope this helps.

- 315
- 4
- 21