[solved] Retrieving synced data (offline)


#1

Hi,

I’m struggling a bit to get out data from my synced realm:

        const user = await Realm.Sync.User.login(`https://${SERVER}`, "user", "pwd");
        Reactotron.log(user, Realm.Sync.User.current);
        const realm = await Realm.open({
          path: 'places.realm',
          sync: {
            user: Realm.Sync.User.current,
            url: `realms://${SERVER}:/pl_6`,
            error: (session,err) => Reactotron.log("SE",err),
            validate_ssl: false,
            fullSynchronisation: false,
            partial: true
          },
          schema: [PlacesSchema]
        }).catch(e => console.error(e));

        const results = realm.objects("places_6");
        const subscription = results.subscribe();

        Reactotron.log("Logging data...", results);

        realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', progressCallback);
        realm.syncSession.addProgressNotification('download', 'reportIndefinitely', progressCallback);
}

This works great and loggs data to the console as it should. The progressNotification is showing progress when subscribing data.

Then, when I turn off wifi and data and try to do the following (basically the same code without th subscription part) nothing happens and no error shows.

async getData() {
  
    const realm = await Realm.open({
      path: 'places.realm',
      sync: {
        user: Realm.Sync.User.current,
        url: `realms://${SERVER}:/pl_6`,
        error: (session,err) => Reactotron.log("SE",err),
        validate_ssl: false,
        fullSynchronisation: false,
        partial: true
      },
      schema: [PlacesSchema]
    }).catch(e => console.error(e));

    const results = realm.objects("places_6");

    console.log("Logging data...", results[0]); // this is not executed
}

Unfortunately I was not able to find sufficient documentation about how to use offline data in realm and now I’m a bit stuck. Anyone an idea what I’m doing wrong?


#2

@386sx Realm.open requires a connection to the server in order to download - if you want to open the same realm that you’ve already downloaded use the new Realm functionality


#3

Many thanks for this hint!

I was stuck here for over 8 hours. I read the documentation back and forth but did not get the difference between .open and new Realm.

Now it’s working as expected. \o/


#4

I’ve also been wondering about this.

If you have an already synced realm and open the realm using new Realm when offline and then the app comes online what will happen? Or the reverse?

What is the recommended way to deal it so that whether you are online or offline everything synchronises cleanly? Docs?


#5

@sambwest new Realm will use the local realm already downloaded via asyncOpen - it matches the Realm URI path - so it will be the same. The problem is that Realm.open requires the app to be online and connected to ROS in order to return


#6

Ian so how can we deal with this part of my question?

I did a few rudimentary tests and none of my results listeners were being called if I initialize the object via new Realm instead of realm.open. e.g.

  1. Sync/download using realm.open
  2. Go offline
  3. Re open via new Realm
  4. Connect the internet again
  5. Add a record via node on the server
  6. Should be synchronizing again and the results listeners called - they weren’t

This leads be to believe we need to do something else to maintain everything when switching from on/off line.

In an ideal world and how I’d expect it to function would be to open via realm.open. If it’s offline but the local realm exists it opens that, then when it goes online again catch up.


#7

@sambwest If you are online - use Realm.open - if you go offline after you first get the realm returned then you can still use the realm reference - it is only when first connecting on app start

If you are offline and you want to open the realm first time (as in on app start) use new Realm API - if you go online that’s fine - just continue using the same reference

You can use the Session state objects to determine if you are online/offline


#8

So what about the listeners? Should they be getting called in this instance? Because mine aren’t - bug?