Local Realm open after synchronized on realm cloud

@Royal_Advice CurrentUser is cached locally under the hood - so even if you go offline you can still open the realm

yes, but if im online, with a currentUser in cache, and i cant authenticate on the server, im unable to open the realm

without access to the cloud, will I not be able to open my realm locally?

I’m not sure how your app is coded but if you are calling .login() or using asyncOpen() these APIs require connectivity in order to function. In fact, if you call login and get rejected this may invalidate your previous valid currentUser

is there anything i can explain better so you can help me?

Are you calling login() or asyncOpen() when attempting to open the realm? What is the error?

I’m check if currentUser exists in cache, if true i call Realm.open(config), else call login.
With my cloud instance offline, i know login will not authenticate, but if im already have an user authenticated in cache with currentUser, when i call Realm.open i get auth error, because i cant make a request to it, but if i turn off the internet, i can connect locally with new Realm(config)

am i doing something wrong?
thanks for responding and for being patient

@Royal_Advice Yes new Realm is the way to go in JS to get around this - Realm.open will attempt to make a call to the server side

look, with Realm.open, if i get auth error from server (in this case with the server offline), will my current user be invalidated? all them, if i have more than 1?

…config …
sync: {
url: serverUrlFull,
error: (error) => {
console.log(error.name, error.message)
}
}

@Royal_Advice The first time you open a realm (basically the first time you run the app) you should use Realm.open() - on subsequent openings of the app you should use the new Realm API to open the already cached Realm on disk. The way to check which way you should open the Realm is to use to use different code paths on app start that are gated by a check to SyncUser.current() != NULL

Hi mate. Can you share me your code to open database locally ? I want my app to open local database when my internet is off. But it doesn’t return anything.

yes sure, tell me the version you are using please

I am using:
realm: 10.0.0-rc.2,
RN: 0.63.2,

The code was for version <= 6
im trying to figure it out to handle data without connection after i have synced a realm in version >= 10

hmm okay ! Please comment here if you manage to find solution. Thanks !

Nothing has changed significantly on that front in v10. Your code should look something like:

async function getRealm() {
    const app = new Realm.App("your-app-id");
    if (app.currentUser) {
        // A user had already logged in - open the Realm synchronously
        return new Realm(getConfig(app.currentUser));
    }

    // We don't have a user - login a user and open the realm async
    const user = await app.logIn(Realm.Credentials.anonymous());
    return await Realm.open(getConfig(user));
}

function getConfig(user) {
    return {
        sync: {
            user,
            partitionValue: "my-partition"
        }
    };
}
2 Likes

look, its seems be easy, im did this, but i have a problema, look here

Thanks for your comment. I have code similar to yours. But unfortunately, when I open my app with internet off, it does not return anything. It just returns empty array.

Same happens with my app. When I open my app with internet off, it returns nothing .

check your cached user logic, use like @nirinchev code

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