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

Expose a Obj::get_parent API #3898

Closed
nirinchev opened this issue Sep 8, 2020 · 14 comments · Fixed by #5737 or #5790
Closed

Expose a Obj::get_parent API #3898

nirinchev opened this issue Sep 8, 2020 · 14 comments · Fixed by #5737 or #5790
Assignees

Comments

@nirinchev
Copy link
Member

Seems like internally, we can get an embedded object's parent via get_all_backlinks, but there's no API to do it, other than very inefficiently getting the path, then trying to traverse it until the first top-level object.

@sipersso
Copy link

sipersso commented May 7, 2021

Not sure if this is the same problem, but I think one of the comments I made here realm/realm-swift#7145 (comment) is very relevant for this.

Without the ability to check backlinks during migrations it is pretty much impossible to migrate a list of realm objects to a list of embedded objects.

The enumeration approach suggested here realm/realm-swift#7145 (comment) does not really work in real world scenarios. I tested this approach to migrate a typically users data (2MB of data) and the migration took 5 minutes to run when all I need to do is to remove orphaned objects that you seem to be able to easily detect once the migration block has completed.

@nirinchev
Copy link
Member Author

@ianpward @fealebenpae can we bump the priority of this? It's been almost a year since we decided to "do it shortly after the launch of embedded objects".

@realm realm deleted a comment from sync-by-unito bot Aug 29, 2021
@papafe
Copy link
Contributor

papafe commented Dec 6, 2021

@ianpward @nirinchev Can we bump this? We just had another request for this functionality, and unfortunately backlinks don't solve all the needs

@nirinchev
Copy link
Member Author

@jsflax @ianpward can someone on the core team pick this up please?

@ianpward
Copy link
Contributor

ianpward commented Jan 4, 2022

@bmunkholm is this on the quick wins list?

@bmunkholm
Copy link
Contributor

@ianpward Yavor is assigned now.

@sync-by-unito
Copy link

sync-by-unito bot commented Jul 3, 2022

➤ Nikola Irinchev commented:

[~[email protected]] is this really in progress? Should we move it to the backlog in order to prioritize it at the next Core planning? Seems like a good intro task for a new hire/new grad.

@nirinchev
Copy link
Member Author

@nicola-cab was this closed by accident? I'm not sure the linked PR addresses the parent API.

@nicola-cab
Copy link
Member

No, it is wanted. Why do you need to get all the backlinks? Why don't you try to let core handle that, instructing core with the new configuration? Especially because the algorithm for migrating tables from top to embedded is quite complicated, surely not something to write in the SDK.
Besides, a schema violation exception will be thrown way before to be able to react in the migration callback and be able to transform all the objects..

@nirinchev
Copy link
Member Author

This request is not related to migrations - it's a convenience API for obtaining the parent of an embedded object. It was part of the scope/design of EmbeddedObjects but was not implemented due to lack of time.

@nicola-cab
Copy link
Member

OK, then reopen it, but why do you need such API? For checking if the embedded object has a parent? for checking if the migration would succeed? All this is now handled in core..

@nirinchev
Copy link
Member Author

No, it's needed to be able to delete an embedded object (among other reasons that I have forgotten since it was 2 years ago). Since we decided to disallow direct deletion of embedded objects (e.g. realm.Remove(myEmbeddedObject)), we figured we should expose an API to get the parent and use that to delete the object - (myEmbeddedObject.Parent as SomeParent).Child = null.

Additionally, it is useful since embedded objects can be contained in multiple parent types and enumerating all of them with the SDK backlinks syntax would be annoying. This is because backlinks exposed in the SDKs are typed, so you'll need to do something like:

var parent = myEmbeddedObject.GetBacklinks<ParentType1>("Child").FirstOrDefault()
    ?? myEmbeddedObject.GetBacklink<ParentType2>("Child").FirstOrDefault()
    ?? ...

@ironage
Copy link
Contributor

ironage commented Aug 29, 2022

@nirinchev we have Obj::get_path() and Obj::get_fat_path() which return the entire path to an embedded object, the top object will be the first element in the returned list. Does this satisfy the need here or do you want something else?

@jedelbo
Copy link
Contributor

jedelbo commented Aug 29, 2022

@ironage I just made support for this

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
9 participants