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
RDART-941: Clear collections in mixed when reassigned #1554
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will core change their behaviour, and hence this is something we need to get back to, or is this by design on cores part?
Pull Request Test Coverage Report for Build 8286821755Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
@@ -3030,6 +3034,10 @@ class _RealmCore { | |||
collectionHandle = mapHandle; | |||
|
|||
final map = realm.createMap<RealmValue>(mapHandle, null); | |||
|
|||
// Necessary since Core will not clear the collection if the value was already a collection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would be better to track what keys are not updated, and then remove only those, instead of removing all followed by setting them again.
I believe those to operations look quite different in the oplog.
Something similar holds for list, ie. only clear the elements in the old-list from length of the new-list and forward (if any), and update the rest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it also depends on what operations are expected when you e.g. set a field to a new collection. In this case, it may be preferable/expected to have the items cleared, then set new ones, rather than removing some items and updating some items.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is definitely a choice, but it would avoid realm/realm-core#7422.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to avoid that though (that issue) as it could make it easier for the SDKs, and the SDKs wouldn't have to call into Core for every item insert. It's definitely an interesting discussion to have, also for Core if we do delegate to them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually the behavior should move to core, as to avoid the repeated calls. I'm more interested in what behavior we want.
Even if we go with clear, then assign, I think this should be handled by core, not the individual SDKs.
Also, to be consistent we should allow setting all collections, not just those in a mixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearing the collection is the correct behavior here rather than replacing some of the elements because of the merge rules for CLEAR vs SET. Here's an example of when this could produce unwanted results:
// Point 1
// All clients have the following state
obj.mixed == RealmValue.from([1]);
// Point 2, Client 1 updates the mixed value
obj.mixed = RealmValue.from(true); // Changes the type
obj.mixed = RealmValue.from([5]); // Reverts back to a list
obj.mixed.clear(); // Clears the list
// Point 3 > 2, Client 2 updates the mixed value
obj.mixed = RealmValue.from(['foo']);
If we didn't clear at point 3, the CLEAR instruction at point 2 would conflict with the SET instruction at point 3 and win over it, so we would lose the changes by Client 2, even though they were made at a later point. With the clear we're doing now, the point 2 CLEAR will be discarded and the SET-s in point 3 will be preserved.
Regarding setting non-mixed collections - the challenge there is that it breaks certain expectations compared to regular objects - e.g.:
final myList = [1];
myPodo.list = myList;
myList.add(2);
// With a plain dart object, you'd get
expect(myPodo.list, hasLength(2));
So there is a strong expectation that what you set is what you get when working with properties. With collections, however, it wouldn't be the case, as we'd need to copy the collection contents over to Core, but wouldn't be able to manage it in-place. This is somewhat mitigated with collections in mixed due to the indirection that Mixed/RealmValue
provides. When you pass in a list to RealmValue.from
, the API implies that you're initializing a RealmValue
with the contents of a collection and not just using the collection as is, so it's less clear whether users should expect this code to work:
final myList = [1];
myPodo.mixed = RealmValue.from(myList);
myList.add(2);
expect(myPodo.mixed.asList(), hasLength(2));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification! Regarding the last example, for the SDKs having the Mixed
wrapped (e.g. the RealmValue
class), I don't think the expectation necessarily is that myList.add(2);
also should be acting on myPodo.mixed
due to the intermediate API that they'd have to go through RealmValue.from()
.
In this case, I'd say that a user's expectations may instead be based on what the API docs of RealmValue.from()
says that it returns (e.g. the same reference as myList
or something else (the latter in this case)).
I think the ambiguity of what users would expect becomes more ambiguous for the JS SDK where the syntax is not differentiated as we don't wrap the Mixed
value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, nice test updates 🙂
Works except when reassigning collections to the same managed value, which depends on realm/realm-core#7422.