Document with optional partition key

I have 4 collections in my databsse, 3 with partition key\value and 1 collection without partition key ( defined as optional )
I can sync in my app ( android ) on the 3 collection using a partition value.
The collection without partition value - according to the docs, should go into “null” partition .
I have tried opening another “realm” with “null” partition , but I not getting any documents when doing so.

Is there an example for using a partition key as optional and sync on “null” partition ? Any suggestions ?


Hey Ran, struggling with the same issue, did you ever resolve this?

@Ron_Dyck were you able to figure this out? I’m stuck in a similar situation.

Yes @alps I did. The result isn’t a null value, but rather an empty string which seems to work fine.
If you look at the tutorial for Quick Start with SwiftUI, in the SyncContentView struct where the partitionValue is set:
OpenSyncedRealmView().environment(\.partitionValue, eliminate injecting the .environment(), so it’s simply: OpenSyncedRealmView()

Attempts at changing the `user.I’d value to null (or .null) isn’t supported, as the type isn’t allowed.
My experience with Swift is limited and I’m not aware of a way to set the value to null.

Of course you’ll need to set your “sync” accordingly using a “Firehose Strategy” as outlined here

Hope that helps,

If you want a nil partition value, you simply provide nil (or .null depending on the context) to the environment. You wouldn’t want to change the user id to nil (nor can you).

1 Like

Thank you @Ron_Dyck , that was helpful.

Hi @Jason_Flax, can you describe how exactly you would do so?
OpenSyncedRealmView().environment(.partitionValue, Changed to : OpenSyncedRealmView().environment(.partitionValue, .null) causes a type error.
My sync settings are set to have the partition key as _partition

I tried the same in C# and get an “unkown error” exception.

string partitionId = "";
config = new PartitionSyncConfiguration(partitionId, user);
realm = Realm.GetInstance(config);


string partitionId = null;
config = new PartitionSyncConfiguration(partitionId, user);
realm = Realm.GetInstance(config);