How to configure only few objects to be synced

we have an existing realm db which in the beginning of the app will be offline and filled with data.
the realm gets intialized with :

   var config = new RealmConfiguration
            {
                SchemaVersion = CurrentVersionDB,
                MigrationCallback = MigrationCallBack()
                ,
            };

to a certain point we want to now add sync for a few (not all ) RealmObjects with our Mongo Atlas. The understanding is that we can set up now certain objects being synced with :

     public static async System.Threading.Tasks.Task<Realm> SyncRealm(Realm existingRealm)
        {
            try
            {
                var myRealmAppId = "mytutorialapp-XXX";
                var app = Realms.Sync.App.Create(myRealmAppId);
                var user = await app.LogInAsync(Realms.Sync.Credentials.EmailPassword("XXX@level28", "PASSWORT"));

                var syncConfig = new FlexibleSyncConfiguration(app.CurrentUser);
                existingRealm.WriteCopy(syncConfig);
                syncConfig.Schema = new[] { typeof(DBImage) };
                var sRealm = Realm.GetInstance(syncConfig);
                sRealm.Subscriptions.Update(() =>
                {
                    var myImages = sRealm.All<DBImage>();
                    sRealm.Subscriptions.Add(myImages);
                });
                await sRealm.Subscriptions.WaitForSynchronizationAsync();
                return sRealm;
            }
            catch (Exception ex)
            {
                var x = ex.Message;
            }
            return null;
        }

When we run this we get a error on all other RealmObjects we did not add as a Subscription “encountered error when flushing batch: error in onBatchComplete callback: error updating resumable progress: timed out while checking out a connection from connection pool: context canceled; maxPoolSize: 20 …”

Basically we are looking for a solutions where we can on demand sync certain RealmObjects with our MongoAtlas and keep most of the data offline. Is that possible with one Realm or is it better to set up a second realm which is handling the sync with Mongo?

Hi @developer_level28 , welcome!

Unfortunately writeCopy does not work with flexible sync, and I think this is the cause of your issues. It should have raised an exception, but it didn’t, so I opened an issue. Unfortunately you can’t just convert your local realm to one that uses flexible sync, so you’ll need to implement the copy yourself.

If you plan to keep your data offline, and then sync only a few things sparsely, I think it would make sense to have two separate realms, one local, and one with flexible sync. Apart from writeCopy, your code makes sense, and you are using subscriptions correctly. If you want, you don’t need to specify the schema for the flexible sync necessarily.
If you are using a synced realm, then you cannot specify what needs to be kept local and what synchronised, as only objects that adhere to the subscription queries will be ever persisted in realm, while the rest will be deleted. So with flexible sync if the data is persisted, is synchronised and viceversa.

I hope I’ve been clear enough!