Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Big risk of loosing data on ClientResetRequiredError #5944
The documentation says it is my responsibility to close realms finished using them. However, in practice, it turns out it is not so easy. When doing a client reset and a realm instance is open, the app will crash and you will loose any unsynced data, so it is very important to be able to close it. This is super tricky to get right!
The documentation says a good practice is to close the realm when the activity is destroyed. But onDestroy is not always called immediately. This makes it almost impossible to guarantee that all realm instances have been closed.
In my case a realm instance from my main activity remains even after calling finish on that activity and starting another responsible for handling Client Resets. In the end I had to put in a thread sleep in my client reset activity in order to give android time to destroy the previous activity before I could execute the reset. Is this really the way to go?
The swift documentation says the following
In the Android Docs, client resets aren't even mentioned. And as it turns out, they are extremely hard to get right. If there is a recommended way to execute a client reset properly, please provide documentation for it.
Version of Realm and tooling
Realm version(s): 5.1.0
Realm sync feature enabled: yes
Android Studio version: 3.1
Which Android version and device: Android 8.1, Nexus 5X
Hmm, we should have some docs on Client Reset, but I cannot find them either right now. I'll investigate that.
But that said, you are right that handling it is far from straightforward. What you are running into is how the activity lifecycle works, so the next activity
We do have some plans for improving the situation though but don't have a definite timeline to disclose yet.
In case of a multi-Activity system, the only way I can imagine is if "possible Realm invalidation" is handled in
Force closing UI Thread Realm is possible with a
I'm honestly just thinking about it now but it does seem tricky
@Zhuinden not sure what you mean. I have a root activity (A) with no references to realm that creates the main activity (B) that opens a realm onCreate and closes it onDestroy (via a Android ViewModel from the new architecture components).
When a client reset is detected, activity B is finished. A gets onActivityResult when B is finished and I start activity C that handles client resets. I tried delaying the reset as much as possible, but even if starting the client reset in onPostResume() of activity C, activity B onDestroy() hasn't been called yet. The realm reference from activity B is the only open reference.
What I did to resolve it was to not start the reset immediately, instead I was forced to do something like this (sample written in Kotlin)
Doesn't feel like a very pretty solution, but it was the only way I could get it working.
Yes, this is an unfortunate side-effect of the Android Lifecycle. The recommendation we normally have for opening/closing in
For now, I think your approach is pretty pragmatic, also considering that Client Reset isn't instantaneous. So you are probably forced to have some kind of spinner anyway.
referenced this issue
Jun 15, 2018
@sipersso So to hear that it causes such problems for you.
We have had to prioritize other issues, but are actually now in active progress on a better Client Reset functionality that will be transparent and avoid data loss. I still can't predict it's completion date, but it's being worked on now.
I think it may be related to that Realm files are still laying around after the user has been signed out.
I get the errors when signing in a user that has previously been signed out without restarting the app. I suspect that since the realm files are still there (they will be deleted on next launch) this is what causing the issue. I guess an ugly workaround would be to do a System.exit() to kill the process after logging out, but it wouldn't be a nice experience.
If I kill the app manually after signing out I don't get any errors. That said, since I do get client reset errors, I think I probably still need to handle it in a nice way.
I'll see what I can do about getting a reproducible case.
It turned out that the problems with client reset was caused by that I was specifying a directory in the sync configuration instead of leaving it to default.
When removing the directory in the configuration builder I didn't get ClientResets when switching anymore :)
Still, because of these problems I am very scared about client reset errors. I posted about client resets in June 2017 expressing my concerns about the difficulty to test and verify the error handling on the Realm Platform: https://forum.realm.io/t/how-can-i-test-my-apps-error-handling/393/
ClientResets are not even mentioned in the docs anymore. Should I just ignore this issue and let the client crash on reset?