How to prepopulate a synced realm?


#1

How do I correctly bundle a prepopulated Realm file into an app and then open and sync future changes for each distinct app user and/or device?

TLDR;

I am struggling to figure out how to prepopulate a synced realm. I’ve followed all of the suggestions I’ve seen online, but whenever I try to open the realm file with a sync configuration (full or query-based), I get the informative error message “Incompatible histories. Expected an empty Realm or a Realm written by Realm Mobile Platform.”

This will be a long post; I apologize. Some additional information, here is my tech stack:

React Native: 0.57.8
realm: 2.21.1
Realm Cloud

Steps I’ve followed up to this point:

  1. I developed the app with a single admin user; this admin has permissions to write data to the Realm Cloud instance and is also the sync user used during development
  2. I then integrated Auth0 and setup JWT authentication which I have verified is working and I see new Auth0 users in the Users table in my Realm Cloud instance
  3. Following advice I’ve found online, I made a copy of the Realm file to bundle with the app (1 opened the Realm file in a node process, 2 used writeCopyTo to generate a new file, 3 add that as an asset in XCode and use copyBundledRealmFiles during app startup)
  4. I can open the copied realm file in Realm studio without issue and I can open the Realm file in the app, without a sync configuration without issue which is what I do for new app users

When a new user registers:

  1. App starts and opens the bundled Realm file without sync
  2. User initiates signup and is directed to the Auth0 registration page
  3. If successful, Auth0 returns the access, refresh and JWT tokens
  4. Create a sync user using the Auth0 JWT
  5. If successful, do a writeCopyTo of the initially opened Realm file, create a sync configuration for the newly created (using user.createConfiguration) and specifying the path as the newly copied realm file
  6. Receive the histories error

I have exhausted my research into the histories error and how to bundle a prepopulated realm file. I have put a lot of time and investment into Realm, but if I can’t solve this seemingly simple problem I’m not sure I can continue using it.

The only solution that I have found to work is what I started with during development: use a single admin user across all app sessions and devices which does not seem viable in a production release.

Please help.