Cannot resolve a reference with multiple candidates (two model instances with same identifier) #2035
-
Bug report
Sandbox link or minimal reproduction code Describe the expected behavior Describe the observed behavior
The same is NOT happening when trying to get a country, even though there are multiple countries with the same identifier, but not in a list of references. It seams that resolving an item by its reference identifier not working if there are multiple items with the same identifier. |
Beta Was this translation helpful? Give feedback.
Replies: 17 comments 1 reply
-
https://github.com/mobxjs/mobx-state-tree/blob/master/README.md#references Identifiers for objects of the same kind must be unique across the whole tree that spawns the whole root store, this is because in their snapshot form references are saved as plain strings /numbers that must point to a single object. There are ways to escape this such as custom references or views that resolve the nodes in a custom way. In case you don't need to use references then you don't need to use identifiers and can just use a simple string or number. Not sure if that's what you meant though 🙂 |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer. I understand what you are saying but there might be situations where a model needs to be used in multiple places in the store. For example, imagine a Anyway, at the time, I solved my problem by replacing the |
Beta Was this translation helpful? Give feedback.
-
Another way to tackle the problme would be to use a custom reference that resolves to a certain array in the store rather than relying on the built-in resolveIdentifier. To be honest I don't use references that much, I usually just use ids as properties and views to resolve them :) |
Beta Was this translation helpful? Give feedback.
-
Btw, if you have any suggestion on how MST should tackle the problem instead of the current implementation feel free to do so! |
Beta Was this translation helpful? Give feedback.
-
This is a useful thread. What I am confused about is this: What is MST's recommended workflow for deep cloning a tree that uses In my case I have a complex structure where a parent references a child and that child references another child, and each contains references to objects below in the tree....... I need to be able to clone an entire partial tree, to make copies of a model instance and all of its children. Seems like I have two options:
It sems like this is not necessarily a simple issue to solve. For example, deep cloning model "associations" is not built into Ruby On Rails, to do that I think you need to write your own code or use a gem like this: |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity in the last 10 days. It will be closed in 4 days if no further activity occurs. Thank you for your contributions. |
Beta Was this translation helpful? Give feedback.
-
I agree, this is an important issue. Having the application break when you try to use a model in 2 locations is really unhelpful... Especially because there is no way to add a 'generic' model property to a model, so you can abstract some logic, and What would be the recommended way to reference generic models? The use case I have is that a user can select elements from different pages to add them to a map, so I'd need an array of references to models, and not know exactly how they fit in the tree... Is there a way to do this? |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar issue (same User appearing in various arrays) except I'm not using references, but just And I was wondering, would it be possible to include a |
Beta Was this translation helpful? Give feedback.
-
It is; if an element appears twice, it means that you didn't create a tree which all other utilities assume (not having a tree for examples makes it impossible to serialize reliably, or to determine unambigously who is the parent of a node). If you find yourself needing to have the same node multiple times in the tree, probably all instances of that except one should be references. For further questions, please open a new issue, as this one is closed already and others won't see the question. |
Beta Was this translation helpful? Give feedback.
-
I wouldn't mind a more elegant solution to this issue, as it's something we've run into ourselves. Re-opening for future consideration. |
Beta Was this translation helpful? Give feedback.
-
@jamonholmgren were you able to find a solution? |
Beta Was this translation helpful? Give feedback.
-
Maybe use types.string instead of types.identifier helps? |
Beta Was this translation helpful? Give feedback.
-
@Navipro70 will that still work with references? |
Beta Was this translation helpful? Give feedback.
-
@10000multiplier |
Beta Was this translation helpful? Give feedback.
-
@10000multiplier and @Navipro70 no it won't that's the whole point of identifier. |
Beta Was this translation helpful? Give feedback.
-
I've released a new library to help with this problem, which I think is pretty cool, well-tested, and pretty well documented. Please check it out and let me know what you think about it! It's called mst-reference-pool and you can find it here: https://github.com/infinitered/mst-reference-pool In a nutshell, it gives you tools to easily create a pool of instances, and then you simply use references from anywhere on the tree. It also comes with a garbage collector so you don't end up with a memory leak when instances are no longer referenced from anywhere. |
Beta Was this translation helpful? Give feedback.
-
Hey @jamonholmgren, et. al., looks like Thanks to everyone who chimed in! |
Beta Was this translation helpful? Give feedback.
I've released a new library to help with this problem, which I think is pretty cool, well-tested, and pretty well documented.
Please check it out and let me know what you think about it!
It's called mst-reference-pool and you can find it here:
https://github.com/infinitered/mst-reference-pool
In a nutshell, it gives you tools to easily create a pool of instances, and then you simply use references from anywhere on the tree. It also comes with a garbage collector so you don't end up with a memory leak when instances are no longer referenced from anywhere.