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

linkingObjects results in recursion in debug mode #1532

Closed
YaoHuiJi opened this issue Nov 26, 2017 · 10 comments
Closed

linkingObjects results in recursion in debug mode #1532

YaoHuiJi opened this issue Nov 26, 2017 · 10 comments

Comments

@YaoHuiJi
Copy link

YaoHuiJi commented Nov 26, 2017

hi, in my app I use realm.objects to get some data, if I turn off the 'Debug JS Remotely' on iOS simulator, everything works fine, realm.objects will return my data. but if I turn on 'Debug Js Remotely', the 'get_property' will be called more than 25000 times until the error ' RangeError: Maximum call stack size exceeded' be thrown, see the screenshot below:
image

after added some debug codes, I found out It seems like the problem is about 'linkingObjects'. my schema looks like that:
class Goal {
static schema = {
name: 'Goal',
primaryKey: 'id',
properties: {
id: {type: 'int'},
title: 'string',
kpis: 'Kpi[]',
}
}
}

class Kpi {
static schema = {
name: 'Kpi',
primaryKey: 'id',
properties: {
id: {type: 'int'},
title: 'string',
goals: {type: 'linkingObjects', objectType: 'Goal', property: 'kpis'},
}
}
}
I go through thousands of 'get_property' requests, and found out that Goal and KPI will call each other recursively, goal.kpis->kpi.gols->goal.kpis->kpi.goals and on and on until 'Maximum call stack size exceeded'. If I turn off the debugger, the recursion is gone, or If I remove the linkingObjects 'goals' of KPI schema, the recursion is gone too.

sorry for my poor english, if It's not clear enough ,please let me know.

Version of Realm and Tooling

  • Realm JS SDK Version: 2.0.2
  • React Native: 0.50.1
  • Client OS & Version: macOS 10.13.1
  • Which debugger for React Native: React Native Debugger
@YaoHuiJi YaoHuiJi changed the title linkingObjects result in recursion in debug mode linkingObjects results in recursion in debug mode Nov 26, 2017
@kneth
Copy link
Contributor

kneth commented Nov 27, 2017

Thank you for reporting and taking the time to debug. We need to write a test to reproduce it and fix it.

@StevenMasini
Copy link

@YaoHuiJi I think that I am getting the same bug as you. In debug mode the main thread freeze and if I compile in Release my Redux actions aren't triggered anymore.

In both case the app doesn't respond properly.

@kneth Here are the two schema involved:

const User = {
  name: 'User',
  primaryKey: 'id',
  properties: {
    id: 'string',
    username: { type: 'string', indexed: true },
    name: 'string',
    avatar: 'string?',
    gender: 'string',
    birthday: 'string',
    latitude: 'double',
    longitude: 'double',
    relationship: { type: 'int', default: relationshipStatusConstants.NON_EXISTENT },
    interactions: { type: 'linkingObjects', objectType: 'Interaction', property: 'members' },
  },
};

const Interaction = {
  name: 'Interaction',
  primaryKey: 'id',
  properties: {
    id: 'string',
    type: 'string',
    weight: 'int',
    timestamp: 'int',
    members: { type: 'list', objectType: 'User' },
  },
};

Interaction has a collection of User via members and each User schema should point at their Interaction via interactions.

Version of Realm and Tooling

  • Realm JS SDK Version: 2.3.3
  • React Native: 0.54.1
  • Client OS & Version: macOS 10.13.3
  • Which debugger for React Native: Reactotron v1.15.0

@kneth
Copy link
Contributor

kneth commented Apr 17, 2018

@StevenMasini Can you describe the operations which causes your Redux actions not to trigger? Can https://github.com/lolatravel/realm-react-redux or https://stackoverflow.com/questions/40167038/using-realm-in-react-native-app-with-redux help you?

@StevenMasini
Copy link

@kneth Thank for your answer. I don't know if realm-react-redux will solve the LinkingObject problem, but it is still a useful resource. We will try to implement it to solve other issues that we are facing with Realm and Redux.

For now, in order to move forward on my project, I just changed the interactions property in User, to a collection of id of Interaction.

After I am not sure what was blocking my Redux actions to trigger, but it seemed like an infinite loop.

@kneth
Copy link
Contributor

kneth commented Apr 24, 2018

@StevenMasini We are eager to hear how it goes when you are using realm-react-redux. It is not a Realm project but we are happy to be part of a community and therefore are we curious to learn how we fit in 😄.

@TheNoim
Copy link

TheNoim commented Sep 10, 2018

This bug has the tag "Reproduction Required":
Here you go:
https://github.com/TheNoim/realm-recursion

It would be nice if this gets fixed.

@ironage
Copy link
Contributor

ironage commented Sep 14, 2018

The default way to serialise an object to JSON will print all properties. Since a realm object with a link also has a backwards link (the linkingObject property) the JSON.stringify() (and probably the react debugger) will hit a recursive loop when printing a realm object. We could potentially provide a version of toJSON() on Realm objects which only prints the forward links but I'm not sure if it would be annoying for debugging not being able to see the linkingObjects property - it would be better than crashing in a stack overflow though.

@TheNoim
Copy link

TheNoim commented Sep 14, 2018

it would be better than crashing in a stack overflow though.

No, I don't think so.
There should be a possibility to serialize a realm object to a string without infinity recursion. Maybe something which prevents to show the link if the same object already go serialized in a higher order.

@ouabing
Copy link

ouabing commented Feb 12, 2019

This also stops me using reactotron, reactotron sending stringify results to reactotron server, but my realm object contains such recursive relationships.

@YaoHuiJi
Copy link
Author

YaoHuiJi commented Jun 12, 2019

See issue #491 and pr #2373 , I have upgrade realm to 2.28.1 and I think It's not that slow now. thanks those guys, really appreciate.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 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