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

Add support for Weak/Strong links aka. Cascading Deletes #5678

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

cmelchior
Copy link
Contributor

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

@Zhuinden
Copy link
Contributor

Oooo this is great 😄 I wonder if this will be in 5.0 along with all the other goodies

@MarkOSullivan94
Copy link

Where can we find what release this will be in or what will be in the next release?

# Conflicts:
#	CHANGELOG.md
#	dependencies.list
#	realm/realm-annotations-processor/src/main/java/io/realm/processor/ClassMetaData.java
#	realm/realm-annotations-processor/src/main/java/io/realm/processor/RealmProxyClassGenerator.java
#	realm/realm-library/src/main/cpp/io_realm_internal_OsObject.cpp
#	realm/realm-library/src/main/cpp/io_realm_internal_OsSchemaInfo.cpp
#	realm/realm-library/src/main/cpp/io_realm_internal_Table.cpp
#	realm/realm-library/src/main/cpp/java_accessor.hpp
#	realm/realm-library/src/main/cpp/jni_impl/android_logger.cpp
#	realm/realm-library/src/main/cpp/jni_util/java_exception_thrower.cpp
#	realm/realm-library/src/main/cpp/jni_util/log.hpp
#	realm/realm-library/src/main/cpp/object-store
#	realm/realm-library/src/main/cpp/util.hpp
@Zhuinden
Copy link
Contributor

Zhuinden commented Jun 11, 2018

¯\(ツ)

@cmelchior
Copy link
Contributor Author

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 😢

@Zhuinden
Copy link
Contributor

Oh I see now :(

realm/realm-object-store#622 (comment)

@alexei-28
Copy link

Realm 5.8.0. - not found cascade delete.
When you implement cascade delete in Realm?
Thanks.

@ChinmayAcharya28
Copy link

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 ?

@Zhuinden
Copy link
Contributor

Zhuinden commented Jun 17, 2019

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

@ashughes
Copy link

According to @roberhofer, this is coming. See #761 (comment).

@rovkinmax
Copy link

any progress?

@bmunkholm
Copy link
Contributor

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.

@rubnov rubnov mentioned this pull request Nov 6, 2020
3 tasks
@bmunkholm bmunkholm marked this pull request as draft February 22, 2022 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for cascading deletes
8 participants