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

How to override == operator and hashcode of a realm model? #1202

Open
santoshakil opened this issue Mar 8, 2023 · 8 comments
Open

How to override == operator and hashcode of a realm model? #1202

santoshakil opened this issue Mar 8, 2023 · 8 comments

Comments

@santoshakil
Copy link

santoshakil commented Mar 8, 2023

Problem

Suppose, I create a RealmModel called _Note. When I generate realm schema it creates an object named Note. Later, we will always use the Note object. But if we need to override the hashcode and == operator then what will be the further step? I've overridden these to _Note object. But it won't work. But, if I override it to the generated Note object, it works. But, modifying generated files is not recommended. here is an example:

`
@RealmModel()
class _Note {
@PrimaryKey()
Uuid? id;

String? title;
String content = '';

@OverRide
operator ==(Object other) => other is Note && id == other.id;

@OverRide
int get hashCode => id.hashCode;
}
`

Solution

No response

Alternatives

No response

How important is this improvement for you?

Dealbreaker

Feature would mainly be used with

Local Database only

@nielsenko
Copy link
Contributor

You are right. We currently don't support that. This is second request we get for this, and it is supported by other SDKs so I feel confident that we will eventually allow this in the Flutter / Dart SDK as well.

However, I cannot give any promises to when.

@santoshakil
Copy link
Author

You are right. We currently don't support that. This is second request we get for this, and it is supported by other SDKs so I feel confident that we will eventually allow this in the Flutter / Dart SDK as well.

However, I cannot give any promises to when.

It is really important for me and many others. can't do many things without overriding these. Please let us know when this feature will be live.

@santoshakil
Copy link
Author

You are right. We currently don't support that. This is second request we get for this, and it is supported by other SDKs so I feel confident that we will eventually allow this in the Flutter / Dart SDK as well.
However, I cannot give any promises to when.

It is really important for me and many others. can't do many things without overriding these. Please let us know when this feature will be live.

Any update on this?

@desistefanova
Copy link
Contributor

Hi @santoshakil!
We would consider adding this options in the future, but in order to rise its priority we would like to know what is the reason of having these options. Can you elaborate more and share with us what is your use case? Are you trying to compare objects from different Realms, or is it something else? What exactly is the reason?
It is very likely we to have some other solution for your use case, if you share more.

@ebelevics
Copy link

ebelevics commented Nov 10, 2023

I just stumbled with this issue, and my guess it is related.
I have unmanaged list of realm objects. And I do list.contains checking selecting/deselecting objects. But every time I call hashCode to same object it always gives me different hashCode, meaning list.contains is always returning false even tho object is present in a list.

I did try using equatable package, but with no luck.

Same issue what I've stated multiple times before, because Realm goal is to use realm objects in app+database layer not separately (unfortunately), this creates given issue, when object preservation is not at that time point necessary.

And only solution thus far I see is creating for this specific use case realm and app models separately.

P.S. just checked at creation, it preserves the hashCode. Will take a deeper inspection, if it will be Realm related, I will address, if not, then sorry for my mistake.

@nirinchev
Copy link
Member

Changing the hashCode doesn't seem correct to me - I have the following test which passes reliably:

  test('RealmObject.hashCode remains stable between invocations', () {
    final team = Team("TeamOne");

    expect(team.hashCode, team.hashCode);
  });

Team here is a RealmObject inheritor. Are you sure you're using the same instance or are you creating different objects and expect them to have the same hashCode?

@ebelevics
Copy link

I have noticed hashCode difference here:

    final resp = await LoadCarServerCalls().getLoadPlan(); // List<LoadPlanPointsResponse>?
    final results1 = resp?.map((r) => r.asRealm).toList(); // Mapping server objects to unmanaged realm object into list
    final results2 = resp?.map((r) => r.asRealm); // Mapping server objects to unmanaged realm object into iterable

    print(results1?.map((e) => e.hashCode)); // (221069079, 102039447, 1026662903)
    print(results1?.map((e) => e.hashCode)); // (221069079, 102039447, 1026662903)

    print(results2?.map((e) => e.hashCode)); // (400072825, 510940315, 492677555)
    print(results2?.map((e) => e.hashCode)); // (398519997, 973606144, 585095231)
    return results1;

I found that mapping toList(), it did fix the issue (for some reason), but Iterable item did change hashcode everytime I use it from Iterable

@nirinchev nirinchev added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Feb 21, 2024
@matheuscsant
Copy link

matheuscsant commented May 16, 2024

Any update on this?
I'm trying use the Set in Dart, and I implemented the == operator in the generated file (and it's a problem, because when I run dart run realm generate the file is generated again), exactly as described in this issue.
In Java for example, it has Lombok annotations, such as (or other ways Lombok - EqualsAndHashCode):

@EqualsAndHashCode(of = "id") //<= I choose wich attribute to use
public class Product implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	@NotBlank
	private String name;
	@NotNull
	private Double price;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants