Skip to content
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

realm object is empty on first run of app #1131

Closed
agersoncgps opened this issue Jul 9, 2017 · 10 comments
Closed

realm object is empty on first run of app #1131

agersoncgps opened this issue Jul 9, 2017 · 10 comments

Comments

@agersoncgps
Copy link

agersoncgps commented Jul 9, 2017

I have an app that communicates with a read-only server hosted realm. I have tried this via a localhost server on my Mac (ROS v1.7.6) as well as an Amazon hosted Linux server (ROS v1.8.1). I have tried this with realm.js version 1.3.1 - 1.8.3.

If I choose Simulator->Reset Content and Settings from the iOS Simulator (to simulate a user who downloads the app for the first time), my console.log for the realm's length equals 0 after calling this function. I can call this function multiple times and its always 0 after wipping out the storage. If I hit comand-r to reload the React Native app inside the simulator and then call this function it returns 1300+ (what is expected), and then works for all future runs of the app.

I also tested this same code on the Android simulator. It returns 0 at all times, no matter how many times I reload. I also tested this same code in a blank node.js project with nothing else loaded besides realm and it corectly returned 1300+ records. All "error" objcts are null or undefined and both "user" and "realm" objects look valid when console.logged.

What could be happening that would explain this behavior?

Realm.Sync.User.registerWithProvider(
            "http://localhost:9080",
            "custom/cgpsauth",
            accessToken,
            (error, user) => {
              console.log("error-- " + error);
              if (!error) {
                Realm.openAsync(
                  {
                    schema: [
                      StudentSchema,
                      ParentPhonesSchema,
                      ParentsSchema,
                      ParentAdressSchema,
                      DirectoryFavoritesSchema
                    ],
                    sync: {
                      user: user,
                      url: "realm://localhost:9080/cgps-app-v1"
                    }
                  },
                  (error, realm) => {
                    // Realm is now downloaded and ready for use
                    console.log(realm.objects("Student").length);
                  }
                );
              }
            }
          );
@Damnum
Copy link

Damnum commented Aug 7, 2017

@agersoncgps I have exactly the same problem on Android emulator. You closed this issue, did you find out the reason or a fix for this?

@agersoncgps
Copy link
Author

The Realm guys were unable to recreate the issue so I thought it was something uniquely messed up in my environment. It appears also to only happen in the simulator and not on device. I will re-open it.

@agersoncgps agersoncgps reopened this Aug 7, 2017
@Damnum
Copy link

Damnum commented Aug 7, 2017

Thanks! I investigated further on the Android emulator.

When I open the app for the first time and open a synced realm like in your example, the realm is empty. But it still gets downloaded into the /data/data/com.appid/files folder. Then, after refresh, it opens and reads this local file successfully.

If I delete the realm files in the /data/data/com.appid/files folder manually, the same error shows up (realm file downloaded but empty).

So I suspect that on the first run the file gets opened too early before the realm is setup correctly. But not sure why and how this happens.

@kristiandupont
Copy link
Contributor

@Damnum are you creating the realm instance via the constructor or with Realm.open/Realm.openAsync? The latter two will make sure that the realm is fully downloaded before handing it over to you.

@Damnum
Copy link

Damnum commented Aug 8, 2017

@kristiandupont yeah I use Realm.open like this:

let realm;

export function downloadDatabase() {
  return new Promise((resolve, reject) => {
    console.log('Logging in user');
    Realm.Sync.User.login(
      AUTH_URL,
      'test@email.com',
      '7%dsf234234!Tzd',
      (error, user) => {
        if (error) {
          reject(`User could not be logged in: ${error}`);
        } else {
           Realm.open({
               sync: {
                   user,
                   url: REALM_URL
               },
              schema: [...]
            }).then(
            theRealm => {
                realm = theRealm;
                if (theRealm.empty) {
                    reject('Loaded realm empty!');
                } else {
                    resolve();
                }
             },
              error => {
                  reject(`Opening realm failed: ${error.message}`);
              }
           );
        }
      }
    );
  });
}

@kristiandupont
Copy link
Contributor

I see. In that case, you might have discovered a bug. If you can come up with reproduction case for us to look at, that would be great.

@Damnum
Copy link

Damnum commented Aug 12, 2017

@kristiandupont turns out the issue only happens when using the Chrome debugger. Same thing with my other problem (now closed): realm/realm-object-server#245

Given multiple tickets are open related to the Chrome debugger, I would suggest to make an additional comment in the docs: That the Chrome debugger is not slow (for me) but really buggy when using it with realm. All sorts of weird bugs happened when using it. I'm using Reactotron for debugging for the time being.

@kneth
Copy link
Contributor

kneth commented Aug 14, 2017

@Damnum Have you tried Genymotion as Android emulator? That's the suggestion in #505.

@Damnum
Copy link

Damnum commented Aug 14, 2017

@kneth This happens on both Android and iOS, regardless of device or simulator. Specifically, Realm Collection Notifications simply stop working when turning on Chrome debugging (in addition to the empty realm problem). More problems with this are documented in #1089, #1095, #729, and others.

@kneth
Copy link
Contributor

kneth commented Aug 18, 2017

@Damnum It looks like we should have a label called "Chrome Debugger" to track our issues with it. I label it as a bug for now, and we will prioritize to give a better debugging experience.

@sync-by-unito sync-by-unito bot closed this as completed Jan 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants