I’m trying to implement Client Reset for Realm Sync, using this popular example.
During this process, from what I understand, we must follow these steps:
- Invalidate and remove all references to realms, realm objects, etc…
Realm.deleteFiles(for:)to delete the files
SyncSession.immediatelyHandleError(clientResetToken, syncManager: app.syncManager). I have no idea what that does. In the example, it is said that this "clears the old realm files and makes a backup in
recovered-realms". But shouldn’t the previous step delete realm files already?
- Copy the automatically generated backup to a safe and known location
Now, I’m struggling with the first step. In the example, there is only one view controller, and few realm references, so it can remove them manually. But in my app, and in many production apps I believe, the situation is way more complex, because:
- The realm is opened in different view controllers (at the same time, if there is a tab bar structure)
- References to the realm objects are kept in a lot of places. For instance, if you have a
UICollectionView, it keeps references to a lot of objects.
- The sync error handler callback is not called directly, so the app has time to load plenty of references to Realm.
So it’s basically very complicated to remove all references to realm. It is in fact stated in the docs that:
In practice, there are two safe times to delete the realm file:
- On application startup before ever opening the realm.
- After only having opened the realm within an explicit
autoreleasepool, which ensures deallocation of all of objects within it.
So I believe a better way to handle client reset would be to store all data needed for a client reset, ask the user to restart the app, and perform the client reset at startup, before any reference to realm is made. But I don’t know how to store the
clientResetToken that is needed in step 3.
Does anybody have a good example of client reset that is actually usable for a complex app?