Invalidation of atom (...) in watched scope (...) is dangerous. #14
-
Hi everyone! I created a UI component with some state marked as [Atom] and upon its distruction I see I am not sure what causes this and how to prevent it. Thank you in advance and keep up the good work! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, the message [Atom] public int X { get; set; }
Atom.Reaction(() => X = 123, debugName: "Y"); DetailedThere are three types of atoms in the library:
However sometimes due to programmer mistake it turns out that the value of atoms is changed directly during the execution of the reaction. For example, this will happen in the following code: public Toggle toggle;
[Atom] public bool IsActive { get; set; }
[Atom] public int Counter { get; set; }
toggle.onValueChanged.AddListener(() => Counter = Counter + 1);
Atom.Reaction(() => toggle.isOn = IsActive ); The Reaction will change the value of SolutionTo solve the problem, you need to wrap the code that changes the state of the atoms in NoWatch. For example, in the UniMob.UI framework all callbacks from UGUI wrapped into NoWatch. I also recommend wrapping all calls to code that we do not control. For example: Atom.Reaction(() => {
var tmp = IsActive;
using (Atom.NoWatch) {
otherGameObject.SetActive(tmp);
}
})
//same as
Atom.Reaction(() => IsActive, tmp => otherGameObject.SetActive(tmp)); If there is a read of atoms in otherGameObject's Awake, then there will be a situation similar to the example above. Alternatively you can strictly control which functions can access atoms and which cannot to avoid this situations.
|
Beta Was this translation helpful? Give feedback.
Hi,
the message
Invalidation of atom (X) in watched scope (Y) is dangerous
means that you are changing the value of anX
atom during the evaluation of anotherY
atom (computed or reaction). The simplest case is:Detailed
There are three types of atoms in the library:
Value
,Computed
andReaction
:Value
atoms must changed as a result of user actions;Computed
atoms must be evaluated based on other computed atoms and value atoms;Reactions
are designed to trigger side effects, such as updating the user interface.However sometimes due to programmer mistake it turns out that the value of atoms is changed direc…