You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm new to Realm and I'm pretty much following all the examples of MongoDB. In the repository of the Realm best practices for react native have been specified functions for handling client reset, realm compaction, transfer progress, and restoring and opening/setting up realms. Using realm context I suspect I don't need functions for restoring and setting up realms, but what about error handling, and what configuration is more suitable with realm context?
The Client Reset function used in the example needs access to the current realm to examine the error type and the path of the realm, but how can I get that information dynamically? I have many realm contexts and I can't know which context I should access, but even then it's not clear to me how to access it in the error function passed to the realm config.
My current set up, which is incomplete, consists of this configuration and methods gathered from the above repo and other mongodb sources:
const RealmContext = createRealmContext({
schema: schemaClasses,
deleteRealmIfMigrationNeeded: true,
shouldCompactOnLaunch: compactOnLaunch,
sync: {
user: app.currentUser,
partitionValue: getSchema(app.currentUser.id).CLASS_NEEDED.partitionValue,
newRealmFileBehavior: {
type: 'downloadBeforeOpen',
timeOutBehavior: 'throwException'
},
existingRealmFileBehavior: {
type: 'openImmediately',
timeOutBehavior: 'openLocalRealm'
},
error: errorSync
}
});
function errorSync (session, error) {
try {
if (error.name === 'ClientReset') {
const realmPath = currentRealm.realm.path;
currentRealm.realm.close();
logger.err(`Error ${error.message}, need to reset ${realmPath}...`);
Realm.App.Sync.initiateClientReset(app, realmPath);
// Move backup file to a known location for a restore
// (it's async, but we don't care much to wait at this point)
// FileSystem.moveAsync({
// from: error.config.path,
// to: realmPath + '~'
// }).catch(error => logger.err(error));
FileSystem.deleteAsync(error.config.path).catch(error =>
logger.err(error)
);
} else {
const alertMessage = error.message.split(' - ')[0];
logger.err(`Received error ${error.message}.`);
Alert.alert(alertMessage);
}
} catch (error) {
logger.err(`Error: ${error?.message || error}`);
Alert.alert(error?.message || error);
}
}
function transferProgress (transferred, transferable) {
logger.info(`Starting uploading of ${transferable}...`);
if (transferred < transferable) {
logger.info(`Transferred ${transferred} of ${transferable}.`);
} else {
logger.info('Transfer finished.');
}
}
function compactOnLaunch (totalBytes, usedBytes) {
const tenMB = 10485760;
logger.info(`Storage Realm: ${usedBytes} / ${totalBytes}.`);
if (totalBytes > tenMB && usedBytes / totalBytes < 0.75) {
logger.info(`Compacting Realm...`);
return true;
}
return false;
}
async function restoreRealm (name) {
if (currentRealm.realm) {
const backupPath = currentRealm.realm.path + '~';
const backupExists = (await FileSystem.getInfoAsync(backupPath)).exists;
if (backupExists) {
const backupRealm = await Realm.open({
path: backupPath,
readOnly: true
});
// This is highly dependent on the structure of the data to recover
for (const [key, schemaObject] of Object.entries(
getSchema(app.currentUser.id)
)) {
if (name !== schemaObject.name) {
continue;
}
const backupObjects = backupRealm.objects(schemaObject.name);
logger.info(
`Found ${backupObjects.length} ${schemaObject.name} objects in ${backupPath}, proceeding to merge...`
);
currentRealm.realm.beginTransaction();
backupObjects.forEach(element => {
currentRealm.realm.create(schemaObject.name, element, 'modified');
});
currentRealm.realm.commitTransaction();
}
logger.info(`Merge completed, deleting ${backupPath}...`);
await FileSystem.deleteAsync(backupPath);
}
}
}
Any suggestions? Do I need to do anything at all or is this taken care of by realm-context? Is the realm configuration correct at all, because WebStorm tells me it isn't because of type incompatibility, but I checked the api documentation and I couldn't find any mismatches.
UPDATE:
No answer needed anymore, went through the documentation carefully again and apparently the things specified in the aforementioned best practices repo are outdated, because manual client reset handling is deprecated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello community,
I'm new to Realm and I'm pretty much following all the examples of MongoDB. In the repository of the Realm best practices for react native have been specified functions for handling client reset, realm compaction, transfer progress, and restoring and opening/setting up realms. Using realm context I suspect I don't need functions for restoring and setting up realms, but what about error handling, and what configuration is more suitable with realm context?
The Client Reset function used in the example needs access to the current realm to examine the error type and the path of the realm, but how can I get that information dynamically? I have many realm contexts and I can't know which context I should access, but even then it's not clear to me how to access it in the error function passed to the realm config.
My current set up, which is incomplete, consists of this configuration and methods gathered from the above repo and other mongodb sources:
Any suggestions? Do I need to do anything at all or is this taken care of by realm-context? Is the realm configuration correct at all, because WebStorm tells me it isn't because of type incompatibility, but I checked the api documentation and I couldn't find any mismatches.
UPDATE:
No answer needed anymore, went through the documentation carefully again and apparently the things specified in the aforementioned best practices repo are outdated, because manual client reset handling is deprecated.
Beta Was this translation helpful? Give feedback.
All reactions