I’m using React Native. Here’s the function that I call to open a connection to Realm.
shouldConnectAsync
is passed in as true
when the partition value is changed (on login) and the database
variable is exported from this file and used throughout the app.
export const connect = async (shouldConnectAsync?: boolean) => {
if (!app.currentUser) {
return Promise.reject(new Error('Current user not found'))
}
const partitionValue = getPartition() // This gets the partition value for the current user
if (!partitionValue) {
return Promise.reject(new Error('Authorization failed'))
}
const config = {
sync: {
user: app.currentUser,
partitionValue,
},
schema,
}
database = shouldConnectAsync ? await Realm.open(config) : new Realm(config)
}