realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.78k stars 574 forks source link

Crash when opening a synced Realm on 10.1.4 #3555

Closed tholhubner closed 3 years ago

tholhubner commented 3 years ago

Goals

I am attempting to open a synced Realm on iOS that I setup using the documentation on the mongoDB website.

Expected Results

Connected to synced Realm and continue in querying and writing.

Actual Results

The app crashes with an EXC_BAD_ACCESS. when running on an iPhone 11 simulator on iOS 14.4. Below is the crash report that I get from the crash, but I have to be honest, crash reporting is something I don't have much experience with. EXC_BAD_ACCESS-logs.txt

Steps to Reproduce

I am opening the app, logging in with an anonymous user and opening a realm that I created on the MongoDB web portal. I followed the instructions for a simply asyncronous connection from the documentation on the website as well. But as soon as I call Realm.open(config) the app crashes on me with an EXC_BAD_ACCESS.

Code Sample

Below is the code that I am using when receiving this error.

Import Realm

import Realm from "realm"

Get the Realm App

export function getRealmApp() {
  const appId = "<APPID>"
  const appConfig = {
    id: appId,
    timeout: 10000,
  }
  return new Realm.App(appConfig)
}

Login with Anonymous user

export async function anonymousLogin() {
  let user
  try {
    const app = getRealmApp()
    const credentials = Realm.Credentials.anonymous()
    user = await app.logIn(credentials)
    return user
  } catch (error) {
    throw `Error logging in anonymously: ${JSON.stringify(error, null, 2)}`
  }
}#### Open the Realm
export async function openRealm() {
  try {
    const anonUser = anonymousLogin()
    console.log("Logged in with the user: ", (await anonUser).identities)
    const config = {
      schema: [driverSchema],
      sync: {
        user: anonUser,
        partitionValue: "_partitionKey",
      },
    }
    const realm = await Realm.open(config)
  } catch (error) {
    throw `Error opening Realm: ${JSON.stringify(error, null, 2)}`
  }
}#### Schema for the Driver
export const driverSchema = {
  name: "driver",
  properties: {
    _id: "objectId?",
    _partitionKey: "string?",
    currentTeam: "string?",
    driverImage: "string?",
    endYear: "double?",
    homeCC: "string?",
    homeCountry: "string?",
    name: "string?",
    number: "int?",
    pastTeams: "string?",
    startYear: "int?",
  },
  primaryKey: "_id",
}

Version of Realm and Tooling

tholhubner commented 3 years ago

@steffenagger this is the crash I was asking about in the #3511 comment I made

kneth commented 3 years ago

@tholhubner

const anonUser = anonymousLogin() -> const anonUser = await anonymousLogin() -- anonymousLogin() is an async function

partitionValue: "_partitionKey" - is the value really _partitionKey? Which partition key is configured server side but the value is set client side

steffenagger commented 3 years ago

Thank you @tholhubner.

@kneth yes, this looks like our validation accepts a Promise, where it should only accept a User- and fails quite ungracefully.

tholhubner commented 3 years ago

@kneth that is the partition value that I have setup in the admin, and in the code for the Schema.

I made the change, adding await to the anonymous login function call, but it still seems I am getting the same crash

kneth commented 3 years ago

@tholhubner The stack trace indicates that anonUser is not a valid Realm.User object. To verify it is, do you get some valid output from console.log("Logged in with the user: ", anonUser.identities)?