realm-js: Fail to create / push an object with linkingObjects to its parent

Questions: In another project, I can use linkingObjects well. But I don’t know why I cannot create or push object with linkingObjects to its parent for now. Once I create or push object linkingObjects to its parent, the UI freezes and the app cannot run. I force quit the app and reenter it. The UI still freezes and the app cannot run.

Please help.

Expected Results

It can update / create the object

Actual Results

It cannot update / create the object. The UI freezes even after force quit and reopen the app. And the app cannot run.

Steps to Reproduce

  1. Run the below code
  2. tap “Click me.”

Code Sample

I modified a bit of your example to simulate my case and I got the same result.

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';

const Realm = require('realm');

const CarSchema = {
  name: 'Car',
  properties: {
    make: 'string',
    model: 'string',
    miles: { type: 'int', default: 0 },
    owners: { type: 'linkingObjects', objectType: 'Person', property: 'cars' } // What I added
  }
};
const PersonSchema = {
  name: 'Person',
  properties: {
    name: 'string',
    birthday: 'date',
    cars: 'Car[]',
    picture: 'data?'
  }
};

export default class App extends Component {

  Update = () => {
    Realm.open({ schema: [CarSchema, PersonSchema] })
      .then(realm => {
        realm.write(() => {
          const myCar = realm.create('Car', {
            make: 'Honda',
            model: 'Civic',
            miles: 1000,
          });
          myCar.miles += 20;
        });

        const cars = realm.objects('Car').filtered('miles > 1000');

        realm.write(() => {
          const myCar = realm.create('Person', {
            name: 'Adrian',
            birthday: new Date(),
            cars: [{
              make: 'BMW',
              model: '8310',
              miles: 200
            }]
          });
        });
        alert(JSON.stringify(realm.objects('Car')))
      });
  }

  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', }}>
        <Text onPress={() => this.Update()} style={{ alignSelf: 'center' }}>
          Click me.
        </Text>
      </View>
    );
  }
}

Version of Realm and Tooling

  • Realm JS SDK Version: 2.0.3
  • React Native: 0.50.1
  • Client OS & Version: Android 7.0, iOS 11 (Both simulator and real device)
  • Which debugger for React Native: Google Chrome (It happens even without debugger)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I’d be happy to lend a you a hand testing on react-native @kneth

@esutton The old one should work but cars: Car[] is the new one way. From 2.0.0, you can also have names: string[] so you don’t have to create a wrapper for primitive types like numbers and strings 😄.

When you have a few minutes, it would be great if you tried to rewrite cars: {type: 'list', objectType: 'Car'} as cars: Car[]. It will only work for 2.0.x.