Is it possible to make a list of embedded objects optional in a Realm Object Model?

In the example here: https://www.mongodb.com/docs/realm/sdk/react-native/examples/define-a-realm-object-model/#define-an-embedded-object-property

How can I make the addresses field optional in the BusinessSchema?

I have tried addresses: { type: "list?", objectType: "Address" },

and addresses: 'Address[]?'

but both seem to raise errors.

To make it required you can just do “addresses: 'Address[]”. I think that it is not a valid schema to make it optional due to some under-the-hood limitations, but curious if there is a reason you need it to be optional? If you are trying to make the list as a whole optional then making it required is no real difference as Realm initializes all lists anyways to empty. If you were trying to make the elements within the list optional, then its worth re-examining why you want that since it is probably safer and better schema design to remove elements from the list that are no longer objects instead of letting them remain there as null entries.

Thanks,
Tyler

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.