Realm sync partition

Is itt mandatory to have handle partition key on the app side(like setting the partition key each time some database insertion task is done) or realm can handle it automatically.
I have set the key partition key on the dashboard while creating sync.

You need to set the partition key when opening the Realm on the Client side, but apart from that you don’t need to manually set it on objects you create. That is handled automatically. In fact, you don’t even need to define the partition key as a property in your model classes.

So for example for my ios app when the app launches, and user logs in i need to set the parition key after login or something different ?

You just need to set it when you open the realm, after that the SDK takes care of setting the partition key on objects for you under the hood.

So the partition key value will hardcoded and will be same for all the users. After that the SDK will handle automatically.
let app = App(id: “myRealmAppId”)
let user = app.currentUser!
let partitionValue = “abc”
var configuration = user.configuration(partitionValue: partitionValue)
Realm.asyncOpen(configuration: configuration) { (result) in
switch result {
case .failure(let error):
print(“Failed to open realm: (error.localizedDescription)”)
// Handle error…
case .success(let realm):
// Realm opened
is this correct ?

that looks correct - yes

hey sorry for bothering again, but this crashes the app since the value is current user is null on app launch.
I think we need to add the partition id once user is logged in.
Can you confirm?
Also the partition key can be anything thing right ?

Yes you definitely need a valid logged in user in order to open a realm - thats a requirement.

The partitionKey requirements are defined here:

Ok now im confused.

Which of the following is correct a or b
a) After login i initialize realm with parition key as “abc” which will be same for all the users
b) After login i initialize realm with partition key as user._id which will be different for all the users

that depends on the use case. The partitionKey field is the same and set for all users of the App and is set in the server side configuration. The partitionkey value is defined on the client and can be the same or different based on the use case. If you want a private realm per user then yes you can set it to be the userId - see more on partitions here -

1 Like

Here’s a couple of specific examples:

a) is correct if you want all users to access the same data. For example, suppose when a user logs into a chat app, they should be able to view the ‘General Chat Room’ where all users can chat together. In that case the initial Realm would configured with the ‘general_chat’ partition.

b) would be correct if, for example, each users app preferences are stored within a separate partition per user. The user logs in, and is initially in the ‘General Chat Room’ per a) but then another realm is accessed containing the users app preferences which is specific to that user. Or, for example, if each chat with another user has its own partition.

Throughout the time a user is using your app, you may have different Realms open at different times to allow access to different data - having multiple chats open for example where each chat is within a different partition (realm). Those partitions (realm) will come and go depending on if the user is still chatting or deleted the chat etc.

Hi @Ian_Ward,

what if my realm backend (app) is used on two different environments like a react-native mobile app (which supports sync natively) and an Angular web application (Realm Web SDK without sync support)?

if I want to insert a document from the Angular application, how should I manage the _partition field? do I have to set it client-side before inserting the document?

thank you.