'Realm won't update on runtime when realm file changed

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?



Solution 1:[1]

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 .

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Artur Hellmann