Add support for Weak/Strong links aka. Cascading Deletes
Closes #1104
This PR adds support for the Core concepts of Weak/Strong links or effectively Cascading deletes.
The semantics are:
- Default relationship are "weak"
- If A has a strong relationship with B, then B is automatically deleted if A removes it's reference or is deleted itself.
- Multiple A's can hold a strong reference to B. B is only deleted when the final A is.
In particular, this solves the most common cascading delete problems we have seen:
- Nested hierarchies of objects where parents own their children.
- Updating list problem: I.e. how to delete objects no longer part of a list after the list has been modified due to a network request.
The public API is a new annotation:
// Model classes
@StrongRelationship
public Person person;
@StrongRelationship
public RealmList<Dog> dogs;
// DynamicRealmObject
boolean DynamicRealmObject.isStrongRelationship("linkField");
// RealmObjectSchema
boolean RealmObjectSchema.isStrongRelationship(String fieldName);
RealmObjectSchema.setStrongRelationship(String fieldName, boolean isStrongRelationship);
API Thoughts:
Cycled through a few ideas before ending on a single @StrongRelationship. This mimics the current behavior of e.g @Required where we only annotate things if different from default behavior. It restricts the number of "types" of relationships we can support, but I don't think that is a problem for the foreseeable future.
Other things attempted:
// More descriptive, can also set it explicitly to weak
@Relationship(RelationshipType.STRONG)
// Reducing the number of annotations by adding it to other annotation proposed in PR about naming
@RealmField(relationship = Relationship.STRONG)
Should we add a @WeakRelationship? Nice for symmetry ala @Nullable/@NonNull but our own API does not have the precedence.
TODO:
- [ ] Flesh out annotation processor unit tests
- [ ] Add DynamicRealm API + tests
- [ ] Add Migration API + tests
- [ ] Merge Sync upgrade from
master
Oooo this is great 😄 I wonder if this will be in 5.0 along with all the other goodies
Where can we find what release this will be in or what will be in the next release?
¯\(ツ)/¯
Sorry. This was put on hold due as we discovered we needed a few more internal changes before it made sense to release this. I don't have a new timeline unfortunately 😢
Oh I see now :(
https://github.com/realm/realm-object-store/pull/622#issuecomment-385618340
Realm 5.8.0. - not found cascade delete. When you implement cascade delete in Realm? Thanks.
Hi, we are using realm for database. We have created Realm classes as per our application database design. In this, There are some models which are having inter related dependancy. But they won't have primary and foreign key relation.
They add row as per defined with db structure but when deletion comes we need to manage that for every model. does realm provides cascasde deletion ?
disclaimer: I am not a member of Realm
They add row as per defined with db structure but when deletion comes we need to manage that for every model.
yep
does realm provides cascasde deletion ?
nope
According to @roberhofer, this is coming. See https://github.com/realm/realm-java/issues/761#issuecomment-499296327.
any progress?
No progress on that yet. We will however release "Embedded Objects" very soon. They will automatically "cascade delete". That will however not cover all needs.