-
Notifications
You must be signed in to change notification settings - Fork 586
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
How to use writeCopyTo
with react query RealmProvider
#6000
Comments
Hi @saravanakumargn and thanks for reporting. The Here would be an example of copying a bundled Realm: // Import a shared configuration that will also be used in your `RealmProvider`
import Realm from "realm";
import {localConfig, syncConfig} from "./myRealmConfig";
export function RealmMigrator(props){
const user = useUser();
useEffect( () => {
// load some sort of logic to determine if you need to do this
if(!migratedToSync){
const originalRealm = await Realm.open(localConfig);
// The `RealmProvider` derives `user` from the `UserProvider`, so we need
// to add it ourselves here.
originalRealm.writeCopyTo({...syncConfig, user});
}
}, [user]);
return props.children;
}; Then the wrapper would be: import {localConfig, syncConfig} from "./myRealmConfig";
import {RealmMigrator} from "./RealmMigrator";
function App(){
return(
<AppProvider id={'app-qa-sbapv'}>
<UserProvider fallback={<AppWelcome />}>
<RealmMigrator>
<RealmProvider {...syncConfig}>
<App />
</RealmProvider>
</RealmMigrator>
</UserProvider>
</AppProvider>
);
}; Let us know if this helps! |
This makes sense @takameyer But I am getting this error when using
|
@saravanakumargn Have you enabled flexible sync in your Atlas dashboard? Should look something like this: |
Yes, I did that already and the schema also synced. Pls, check the screenshot Both local and sync modes are working fine separately. I am getting this error on copy
|
@saravanakumargn I apologize, but I believe my example is incorrect: // Import a shared configuration that will also be used in your `RealmProvider`
import Realm from "realm";
import {localConfig, syncConfig} from "./myRealmConfig";
export function RealmMigrator(props){
const user = useUser();
useEffect( () => {
// load some sort of logic to determine if you need to do this
if(!migratedToSync){
// The `RealmProvider` derives `user` from the `UserProvider`, so we need
// to add it ourselves here.
const syncedRealm = await Realm.open({...syncConfig, user});
syncedRealm.writeCopyTo(localConfig);
}
}, [user]);
return props.children;
}; The |
@takameyer Thanks for looking at this. The use case is anytime I can convert local to sync and sync to local vice-versa. So, the first time when I open the app it's non sync(local). after some time(if premium users i need to copy local to sync and continue the app) |
@saravanakumargn I have reached out to our core team. It appears we don't support the automatic conversion through What you could do instead is open the local realm and the sync realm and copy the data directly in your application code, basically doing the conversion yourself. Keep in mind, you must add a flexible sync subscription first and then you can start writing to the synced realm. This method does offer a deal of flexibility as to what data you want to copy over and possibly update any fields (such as userId). Hope that helps! |
Thanks for your help & support in this issue @takameyer. Let me check how to do this. |
As this is expected I am closing this ticket. |
@takameyer are you talking about this issue? realm/realm-core#5798 |
@saravanakumargn That issue was referring to making the error a bit more helpful. The project is on our roadmap for the year, but there's nothing publicly posted at the moment. Sorry I can't provide any further information. |
@saravanakumargn hey, I'm also looking for a consistent way to migrate a local realm to a sync realm. Did you manage to get this working? If you did, do you mind sharing an example code snippet? My current approach is to send the local data manually to Atlas, then logout the local user and switch to the sync realm, which should download the data from Atlas. So the steps would be:
Unfortunately sometimes this does not work (sometimes it does). I guess there is maybe a race-condition between downloading the data and opening the sync realm... not really sure. |
Nevermind, I figured it out :) I just had to skip the exporting of the data to Atlas and instead after switching to the sync realm, open the local Realm and save the local data to the sync realm... and of course close the local Realm afterwards. <AppProvider>
<UserProvider>
<RealmProvider sync={flexSyncConfig}>
<App />
</RealmProvider>
</UserProvider>
</AppProvider> And in if (shouldMigrate) {
const localRealm = await Realm.open(localRealmConfig);
const arr = localRealm.objects("SomeType");
realm.write(() => {
arr.forEach(obj => {
realm.create("SomeType", obj)
})
})
} As @takameyer mentioned earlier, It's important to call |
How frequently does the bug occur?
Always
Description
How to use
writeCopyTo
with react queryRealmProvider
? when I use react query hot i can open Realm and copy from local to sync?Stacktrace & log output
No response
Can you reproduce the bug?
Always
Reproduction Steps
No response
Version
@realm/react 0.5.1, "realm": "^11.10.1",
What services are you using?
Atlas Device Sync
Are you using encryption?
No
Platform OS and version(s)
"realm": "^11.10.1",
Build environment
Which debugger for React Native: ..
Cocoapods version
No response
The text was updated successfully, but these errors were encountered: