Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose realm ref when using RealmProvider in @realm/react #4571

Closed
thespacemanatee opened this issue May 12, 2022 · 7 comments · Fixed by #4575
Closed

Expose realm ref when using RealmProvider in @realm/react #4571

thespacemanatee opened this issue May 12, 2022 · 7 comments · Fixed by #4575

Comments

@thespacemanatee
Copy link
Contributor

thespacemanatee commented May 12, 2022

Problem

Hello,

Thank you for this amazing service. When opening a Realm, one could handle client resets with the following code:

const handleRealmSyncError: Realm.ErrorCallback = useCallback(
  (_session, error) => {
    if (realmRef.current) {
      if (error.name === 'ClientReset') {
        realmRef.current.close();
        Realm.App.Sync.initiateClientReset(realmApp, realmPath);
        realmRef.current = null;
      }
    }
  },
  [realmPath],
);

and opening a Realm with the following sync config:

sync: {
  user: currentRealmUser,
  error: handleRealmSyncError,
},

But with @realm/react, we cannot use the useRealm hook in the same component where RealmProvider is declared, and thus there is no way to close the Realm in the error handler.

This enhancement should be a high priority as the production app would simply break in the event of a client reset and users would have to reinstall a newer version of the app with the updated schema.

Solution

As mentioned by @takameyer, we should provide a ref prop to the RealmProvider and set the value when it’s rendered, so that it can be accessed in the error handler in the same component.

How important is this improvement for you?

Dealbreaker

@takameyer
Copy link
Contributor

@thespacemanatee Thanks for creating this feature request. We will report here when we have an update.

@takameyer
Copy link
Contributor

@thespacemanatee The PR for this is currently under review. However, I wanted to ask about your client reset strategy. We have recently released an automatic client reset method and deprecated the usage of Realm.App.Sync.initiateClientReset. Can you solve the same problem using clientResetBefore and clientResetAfter?

@thespacemanatee
Copy link
Contributor Author

thespacemanatee commented May 16, 2022

@takameyer thank you so much for the quick turnaround time for this, I will try the method that you linked and report back. As far as I know from the documentation, that technique does not work when there's a breaking change in the schema, but this should not happen very often in production.

@LukeStorry
Copy link

LukeStorry commented Nov 25, 2022

Hi, I'm not sure how we can use this ref - can you confirm a app is accessible?

We're having issues getting anything out of current - it appears to always be either null, an empty object, or {"syncSession": undefined} when logging the results.

For context, we're trying to use this realmRef to get progress state to show in the fallback component, using something like:

realmRef.current?.syncSession.addProgressNotification('download', 'reportIndefinitely',
  (transferred, transferable) => setSyncProgress(transferred / transferable)
)

@takameyer
Copy link
Contributor

@LukeStorry Can you provide more context on how you are setting your realmRef? And also please create a new issue describing exactly what you experiencing and how to reproduce it. I have recently used both the realmRef and appRef successfully.

@LukeStorry
Copy link

Yeah that code snippet is pretty much what I already had - have you tried running it?

Issue is the useEffect only fires the one time - when realm is null - and so never gets past the if (realm) check.

@takameyer
Copy link
Contributor

@LukeStorry The snippet was deleted, as I just realized this doesn't work. I think the issue is that the underlying provider is awaiting Realm.open, which doesn't resolve until the sync has finished downloading. Please make a new issue so we allocate time to get this fixed.
I'm considering passing the download progress to the fallback component. Could be something like:

<RealmProvider
   fallback={({progress: {transferred, transferable}}) => ...}
   ...

But we should also make the realmRef usable.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants