Unable to open synced realm offline

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!

2 Likes

If anyone is still looking, I’ve managed to solve my issue using the response to this Stack Overflow post.

Here are the changes I had to make to enable the app to work offline. The implementation is related to the doc page called ‘Sync changes in the background’. Basically without these changes, the app hangs with the cog spinning when offline but nothing is shown.

I personally found the doc (including React Native Realm Sync tutorial) not very helpful to solve this issue. I hope others can benefit from this answer!

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.