-
Notifications
You must be signed in to change notification settings - Fork 165
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
RealmObject should overload GetHashCode #1650
Comments
Hi @nielsenko |
Seems others are struggling with the same thing. #1600, maybe we could get a copy of the object, an unmanaged copy. The row-index in the ObjectHandle, can that be used as HashCode for the RealmObject? After deletion will the row-index be reused or is it always incremented when new objects get an index. |
@janniklind you can't get a copy of the object, because when you factor relationships and backlinks you may end up pulling the entire database in memory :) That being said, tools like AutoMapper allow you to specify a mapping strategy and copy the relevant fields of a managed object to memory. @nielsenko the GetHashCode method should be fairly easy to implement but it's very unlikely that it can be stable between managed and unmanaged objects, e.g. if I have var foo = new Foo();
var unmanagedHash = foo.GetHashCode();
realm.Write(() => realm.Add(foo));
var managedHash = foo.GetHashCode();
// managedHash != unmanagedHash And as Brian said, we'd be very happy to review a PR that adds this. |
Well, since you ask so nicely :-) I have been looking a bit at the source. I get two fody related errors when trying to build master with vs4mac, but I'll try anyway. My idea is to use delegate from As I recall |
Note that it’s entirely possible to get two or more distinct handles pointing to the same row in the database. |
Yes, but the underlying
stays the same, as long as the object is alive, and the realm db is not compacted, which never happens on the fly, or? |
The row index is merely a sequential integer. We use The best way to overcome the ambiguity of the row index is to also use the pointer to the table it belongs to via Once Core switches away from row indexes and assigns every row a unique id that will be stable across versions instead we should be able to implement |
@fealebenpae is there any ETA for the unique id instead of indexes? |
@janniklind early next year, I guess. It's a major change. |
@fealebenpae You say that row indexes are not guaranteed to be stable across transactions, and, if I understand you correctly, including To have a stable hash code across transactions I need to take into account the version of the realm. I must admit, that it is not apparent to me how to to build a stable hash from a changing row index and realm version. For the use cases I see, I need the hash to be stable while the process is running. It need not be stable across processes, so I feel it should be possible to calculate a hash without a unique row id, if just there is something that is stable per object while the process runs. Anyway, I have another idea.. To work around the missing overload of |
Primary keys would be a lot better, I think, yes. |
I can look into adding the weaver logic later this month if no one wants to beat me to it. |
Goals
Feature request: Have
RealmObject
overloadGetHashCode
to match the overload ofEquals
. II think it i "bad" c#-style not to do so - it even gives a usage warning: CA2218: Override GetHashCode on overriding Equals on compilation (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2218-override-gethashcode-on-overriding-equals).
For extra bonus, have
GetHashCode
work even ifIsValid == false
if possible. This will allow us to cleanup any mapped objects we may have stored in hash tables.The text was updated successfully, but these errors were encountered: