Client tried to connect using flexible sync before initial sync is complete

I am having terrible time trying to stabilize Sync. After I synced my offline database it worked once, but when I copied the resulting .realm file from Documents/mongodb-realm folder to a custom directory (and changed the config to point to that file) - everything stopped working. Since then I tried everything, including copying the file back to initial directory, adding/removing subscriptions, each time I get this error and it just keeps retrying and my app - hanging.

I read in docs about this “ErrorInitialSyncNotCompleted” error, but I just don’t understand what is it saying how can the inital sync complete if I don’t connect? I waited for hours, literally and it doesn’t go away, and I have no changes between local and remote databases.

Also tried to put subscriptions into initial subscriptions, termination and re-activation of sync and everything else I could think of.

Here’s the connection code:

        private void AddSubscriptionsToRealm(Realm instance)
            var subscriptions = instance.Subscriptions;
            subscriptions.Update(() =>

        private async Task ConnectToSyncedDatabase()
            Logger.LogLevel = LogLevel.All;
            Logger.Default = Logger.Function(message =>
                Debug.WriteLine($"APP: Realm : {message}");

            var myRealmAppId = "id";
            _app = App.Create(myRealmAppId);
            _user = await _app.LogInAsync(Credentials.Anonymous());

            _config = new FlexibleSyncConfiguration(_user, Path.Combine(FileService.AppDataDirectory, FileService.DatabaseName));
            var realm = Realm.GetInstance(_config);
            // Hangs here
            await realm.Subscriptions.WaitForSynchronizationAsync();

Why do you need to move the file around on the local client directory? I’m not surprised that that doesn’t work that well - what are you trying to do?

Likely if you just want to start syncing again you can just wipe your local client by uninstalling and then reinstalling the app to continue syncing again.

I needed to test, that it will work once I ship the .realm file with my app to other device. There is no point in having sync if it works only on my computer.

I did wipe everything and retried - didn’t help, and when I tried to remove the file and let Sync create it from the server, it kept on increasing its size and I stopped it when it flew after 250Mb, as my initial database file is just 44Mb.

Anyways, this tech is super fragile, I think maybe I should try creating my own Sync layer using Web SDK, though maybe it’ll have same issues…

UPD 1:
Now I’m getting this error:

Info: Connection[1]: Session[1]: Received: ERROR “Bad client file identifier (IDENT)” (error_code=208, try_again=false, error_action=ClientReset)

If you are trying to bundle a .realm file to ship in another app you’ll want to use the WriteCopy() API and follow the instructions under Bundling a Realm file

1 Like

I see, didn’t know that, thank you!

But what can I do now? Can I somehow download the database from server? The normal Sync with removed local file creates a file and keeps writing to it past 200MB as I described, should I wait for it to finish?

Yes let it finish downloading and then you can call compact to reduce file size -

But also the writeCopy() API will do that automatically for you.

1 Like

I see, ok, thank you, I’ll see what happens and come back :slight_smile:

So at this time the sync process created a 3Gib, out of original 49Mb file, is that okay? How big this file can get? It’s still running - expanding.

I waited until it got 6Gb - there’s clearly something wrong with this sync process, it doesn’t log any errors, just keeps on writing stuff.

This is ridiculous :smiley:

I tried to create a new Atlas App and start from clean database, guess what? - sync doesn’t work even on fresh and clean database! Seriously with this amount of work and nerves I would have already created my own layer, actually I think I’ll just use a MySQL database and write a sync layer - it’ll be almost free and I will know what’s going on when something doesn’t work :smiley:

So what’s happening here is that sync is downloading all operations from the time you started sync until now which is inflating your storage size compared to your actual state size - we need these operations in order to have a deterministic conflict solution algorithm. Your can read more about this and reducing the size in production here -

What you can do now, since you are in production, is to terminate and re-enable sync - since this will sync down to the client the most compact version of the sync history.

I don’t believe that that is what’s happening, how could I have made 6Gb of changes in 24 hours since creation of server app - there is no possibility of that, I only changed 3-4 documents for testing and I terminated and re-enabled sync right before doing the sync, this is clearly a bug.

Maybe I am simply doing something wrong, do I need partition key for Flexible Sync for isntance?

Do I need to subscribe to realm entities each time I get the instance? Because working with async/await many times I need to call Realm.GetInstance to use realm in the current thread, do I need to do anything with subscriptions in these cases?

Would be great if logs contained progress or operation name that’s running instead of the constant ping-pong

Maybe I should get a paid subscription to access tech support… I really like the concept and writing the sync layer on my own, although I could do that - it’s too much time.

Now, it doesn’t even work with empty and newly created apps, hangs each time on WaitForSubscriptions :frowning:

As the last resort I removed the entire cluster with everything, installed the nightly - latest build of Realm, cleaned the project, restarted my PC, removed the local database and just tried to connect to a clean database - it creates the schema, but that’s all, when it reaches

 await _realm.Subscriptions.WaitForSynchronizationAsync();

the app hangs indefinitely - yes, I waited for hours.

I am going to postpone my transition to online version until I finish the other things, I hope in a few weeks this issue gets resolved somehow.

@Ian_Ward thank you for your effort.