Hi @Desislava_St_Stefanova,
The change is I made was changing the property to an embedded object type:
Property 'userSearches.reference' has been changed from '<references>' to '<ReferenceRealmEO>
As I’ve mentioned, the clientResetHandler is not invoked. The log does not print.
I did remember when I did my first reset, the log did show up, but my reset code only printed the logs, so nothing was done about the reset. Now, subsequent reset does not seem to invoke the resetHandler.
I have a singleton RealmApp class below (the error occurs in openRealm method):
/// A wrapper singleton instance of a realm app for convenience of access to the realm app
class RealmApp {
static Logger log([Set<String> tags = const {}]) => LogFactory.infrastructure.service<RealmApp>(tags);
///////////////////////////////////// STATIC
static final RealmApp instance = RealmApp._internal();
static Future<void> initialiseApp(AppConfiguration realmAppConfiguration) async {
log().d('initializeApp : initialising RealmApp');
instance._app = App(realmAppConfiguration);
log().d('initializeApp : done');
}
///////////////////////////////////// INSTANCE
RealmApp._internal();
/// Holds a single instance of the realm app which must be initialized before use
late final App _app;
/// Holds the current realm for used throughout the app
late Realm _realm;
Realm get realm => _realm;
Future<Realm> openRealm(User user) async {
log().d('openRealm : opening realm for ${user.profile.name}');
final config = _flexibleConfig(user);
try {
_realm = Realm(config);
} catch (e) {
log().d('openRealm : $e'); // the error is thrown here and not in the clientResetHandler
rethrow;
}
log().d('openRealm : opened realm for ${user.profile.name} at path ${_realm.config.path}');
log().d('openRealm : updating sync subscription length : ${_realm.subscriptions.length}');
// Add subscription to sync all objects in the realm
_realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.add(_realm.all<TaskRealm>());
mutableSubscriptions.add(_realm.all<ActualBodyCompRealm>());
mutableSubscriptions.add(_realm.all<TargetBodyCompRealm>());
mutableSubscriptions.add(_realm.all<UserPreferencesRealm>());
mutableSubscriptions.add(_realm.all<UserSearchRealm>());
});
log().d('openRealm : updated sync subscription length : ${_realm.subscriptions.length}');
log().d('openRealm : waiting for sync subscription');
await _realm.subscriptions.waitForSynchronization();
log().d('openRealm : done');
return _realm;
}
Configuration _flexibleConfig(User user) => Configuration.flexibleSync(
user,
_flexibleSyncSchema,
syncErrorHandler: (syncError) {
log().d('syncErrorHandler : ${syncError.category} $syncError');
switch (syncError.category) {
case SyncErrorCategory.client:
break;
case SyncErrorCategory.connection:
break;
case SyncErrorCategory.resolve:
break;
case SyncErrorCategory.session:
break;
case SyncErrorCategory.system:
break;
case SyncErrorCategory.unknown:
break;
}
},
clientResetHandler: RecoverOrDiscardUnsyncedChangesHandler(
onBeforeReset: (before) {
log().d('clientResetHandler : onBeforeReset');
},
onAfterRecovery: (before, after) {
log().d('clientResetHandler : onAfterRecovery');
},
onAfterDiscard: (before, after) {
log().d('clientResetHandler : onAfterDiscard');
},
onManualResetFallback: (error) {
log().d('clientResetHandler : onManualResetFallback');
},
),
);
/// Logs in a user with the given credentials.
Future<User> logIn({required Credentials credentials}) async {
log().d('logIn : logging in with ${credentials.provider.name} credentials');
final user = await _app.logIn(credentials);
log().d('logIn : logged in as ${user.profile.name}');
await openRealm(user);
log().d('logIn : opened realm for ${user.profile.name}');
return user;
}
/// Logs out the current user, if one exist
Future<void> logOut() async => currentUser?.logOut();
/// Gets the currently logged in [User]. If none exists, `null` is returned.
User? get currentUser => _app.currentUser;
/// Gets all currently logged in users.
Iterable<User> get users => _app.users;
/// Removes a [user] and their local data from the device. If the user is logged in, they will be logged out in the process.
Future<void> removeUser({required User user}) async {
return _app.removeUser(user);
}
/// Deletes a user and all its data from the device as well as the server.
Future<void> deleteUser({required User user}) async {
return _app.deleteUser(user);
}
/// Switches the [currentUser] to the one specified in [user].
Future<Realm> switchUser({required User user}) async {
_app.switchUser(user);
return realm;
}
}
Here is the log for the above code which proves that the error is caught and not handled by the flexible sync configuration:
I/flutter ( 5782): [RealmApp] : logIn : logging in with jwt credentials
I/flutter ( 5782): [RealmApp] : logIn : logged in as YdkTtwVn9XM1UxkswW6UPvQYM7B3
I/flutter ( 5782): [RealmApp] : openRealm : opening realm for YdkTtwVn9XM1UxkswW6UPvQYM7B3
I/flutter ( 5782): [RealmApp] : openRealm : RealmException: Error opening realm at path /data/data/fit.tick.fitapp.dev/files/mongodb-realm/fitapp-dev-kkccq/63f2c277511cef8ea5219c0f/default.realm. Error code: 18 . Message: The following changes cannot be made in additive-only schema mode:
I/flutter ( 5782): - Property 'userSearches.reference' has been changed from '<references>' to '<ReferenceRealmEO>'.
I/flutter ( 5782): [INFO] Realm: Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, client reset = false
[log] RealmException: Error opening realm at path /data/data/fit.tick.fitapp.dev/files/mongodb-realm/fitapp-dev-kkccq/63f2c277511cef8ea5219c0f/default.realm. Error code: 18 . Message: The following changes cannot be made in additive-only schema mode:
- Property 'userSearches.reference' has been changed from '<references>' to '<ReferenceRealmEO>'.
#0 _RealmCore.throwLastError.<anonymous closure> (package:realm/src/native/realm_core.dart:119:7)
#1 using (package:ffi/src/arena.dart:124:31)
#2 _RealmCore.throwLastError (package:realm/src/native/realm_core.dart:113:5)
#3 _RealmLibraryEx.invokeGetPointer (package:realm/src/native/realm_core.dart:2784:17)
#4 _RealmCore.openRealm (package:realm/src/native/realm_core.dart:599:32)
#5 Realm._openRealm (package:realm/src/realm_class.dart:194:22)
#6 new Realm._ (package:realm/src/realm_class.dart:149:98)
#7 new Realm (package:realm/src/realm_class.dart:147:38)
#8 RealmApp.openRealm (package:fitapp/infrastructure/mongodb/realm/app/realm_app.dart:36:16)
#9 RealmApp.logIn (package:fitapp/infrastructure/mongodb/realm/app/realm_app.dart:104:11)
<asynchronous suspension>
#10 FirebaseRealmAuthService._realmLogIn (package:fitapp/infrastructure/hybrid/auth/firebase_realm_auth_service.dart:99:5)
<asynchronous suspension>
#11 FirebaseRealmAuthService._watchAuthStateChanges.<anonymous closure> (package:fitapp/infrastructure/hybrid/auth/firebase_realm_auth_service.dart:60:13)
<asynchronous suspension>
I/flutter ( 5782): [INFO] Realm: Connected to endpoint '52.64.157.195:443' (from '10.0.2.16:40040')
I/flutter ( 5782): [INFO] Realm: Verifying server SSL certificate using 155 root certificates
I/flutter ( 5782): [INFO] Realm: Connection[1]: Connected to app services with request id: "64236ef23e632940cea87942"
D/EGL_emulation( 5782): app_time_stats: avg=36.71ms min=12.31ms max=123.14ms count=27
D/EGL_emulation( 5782): app_time_stats: avg=16.68ms min=9.88ms max=21.42ms count=60
I/flutter ( 5782): [INFO] Realm: Connection[1]: Session[1]: Received: ERROR "Invalid query (IDENT, QUERY): failed to parse query: query contains table not in schema: "userSearches"" (error_code=226, try_again=false, error_action=ApplicationBug)
I/flutter ( 5782): [INFO] Realm: Connection[1]: Disconnected