Realm Client Reset errorHandler returns NIL session.configuration()

I’m trying to implement a custom client reset error handler and I’m getting NIL for session.parentUser() and session.configuration().

I started from the “RealmPractices” GitHub repo where there is a nice example on how to intercept the clientReset and backup the partition. Since I have 2 partitions (1 public, 1 private), I need the session info to figure out which one is the private one and backup it.

My flow is simple:

  1. Add the custom errorHandler
  2. realm.login(…) (if needed, else return app.currentUser
  3. Realm.asyncOpen(configuration: realmApp.currentUser!.configuration(partitionValue: “public”))
    3.1 call will fain (since I made some schema modifications on the Realm App)
    3.2 errorHandler is invoked (as expected). The problem is it’s missing the session data.

Any idea? I checked the realmApp.currentUser and the user is still logged in.

Hi @Andrei_Matei,

That’s correct: as the asyncOpen(…) call fails before the error handler is called, so the session isn’t valid anymore at that point. You could still handle the error in the .failure(error) case of the switch, and take note of the configuration (that’s probably what you need) at that point, as you likely have it at hand. When the error handler is called right after, you can use the stored configuration.

The error handler is called with a valid session in other cases:

  • When sync is reset while the app is running, and you’ve a session opened already
  • When you open the realm synchronously, i.e. with Realm(configuration: …): that is the suggested way when you’ve already a fully functional local realm, as possibly only a few transactions need to be downloaded. If you don’t have a compelling reason to open realms asynchronously after the first sync (that would never get a Client Reset, obviously), you should open them synchronously. That’s also what the sample code does, basically.

Hope the above helps clarifying the situation, feel free to add more details, if additional information is required.