I am making a React Native app using Realm Sync and I am in dire need of guidance regarding how to use it offline.
This is the bare bone of my code:
import Realm from "realm";
import { getRealmApp } from "../getRealmApp";
// Access the Realm App.
const app = getRealmApp();
const AuthProvider = ({ children }) => {
const [user, setUser] = useState(app.currentUser);
useEffect(() => {
if (!user) {
return;
}
console.log('> User: ', user.id) // This logs correctly offline
const config = {
schema: [User.schema],
sync: {
user: user,
partitionValue: `user=${user.id}`,
error: errorSync,
},
};
Realm.open(config).then((userRealm) => {
console.log('> User realm open') // This does not log when offline
const users = userRealm.objects("user");
const company_id = users[0].company_id
if (company_id) {
setCompanyID(company_id)
}
}
}, [user]);
//... then a company realm is open to access data and show on Home Screen.
}
This works fine when I have connectivity but when I don’t, the userRealm does not seem to open, despite having found the user.id from the app.currentUser (as proven by the logs).
I have read in places things about opening a realm synchronously versus asynchronously but I am not sure how to do this in my case (if indeed it is where the problem comes from).
Many thanks in advance for any help here!