error-'Cannot write to class Item when no flexible sync subscription has been created.

I am trying to develop an application that will upload objects of different types into MONGO DB using REALM. For example, in the GIT code, I am trying to upload objects of the ITEM type and an object of the DOG type. If I only upload an object of type DOG or an object of type ITEM then the code works and uploads the object to MONGODB. But if I run my application and the initial object is of type Dog then I manage to upload the object to MONGODB, but if I change the object from DOG to ITEM and try to upload an object of type ITEM to MONGODB (meaning now I changed the object from DOG to ITEM) I get the following error:
Realms.Exceptions.RealmException: ‘Cannot write to class Dog when no flexible sync subscription has been created.’

That is, the error occurs when I try to replace the object that I want to upload to MONGODB while the application is running.

I am adding my git repo :

There are two things at work here.

First, To sync using flexSync, objects need to have a subscription. This is done in code and would looks something like this in Swift

    let user = try await getUser() //get a current user
    var config = user.flexibleSyncConfiguration() //get the flex sync config
    config.objectTypes = [PersonClass.self] //define the object(s) we want to sync
    let realm = try! await Realm(configuration: config, downloadBeforeOpen: .always) //open realm to sync

Even if you don’t use Swift, you can see the objects we want to sync are [PersonClass.self].

Likewise if I wanted to sync Persons and Dogs it would be an array

config.objectTypes = [PersonClass.self, DogClass.self]

Second, the bad news: all sync’ing is depreciated and will not be available after Sept 2025. That means that anything that’s sync’s now, will not work after that date.

Furthermore, ALL SDK’s have had sync’ing removed. Starting with v20.0.0

All Atlas App Services and Atlas Device Sync functionality has been removed. Users of Atlas Device Sync should pin to v10.

Even the future of Realm (local only) is unknown so developing with it is questionable until more information is provided.

I tried to change the code accroding to what you suggested but I still get the same error .

Understood.

While describing the issue is helpful, it doesn’t provide a clear picture of why your code isn’t working. The best questions include a description of the issue, a minimal example of the code that doesn’t work and your troubleshooting. That last bit is the most important part. For example, adding a breakpoint and stepping through your code line by line, inspecting your vars and code execution along the way can be very revealing, and including that in a question helps us to understand the failure.

Also, because Realm is winding down, you may want to consider a post on StackOverflow instead of here where there may be more eyes on the question.