开发者

How do I know if the user made changes so I can save state when needed?

开发者 https://www.devze.com 2023-02-14 12:12 出处:网络
My app is kind of an editor where the user can drag and drop objects over a canvas, and change N properties on the objects. How can I know if the user has made changes on the app in order to prompt hi

My app is kind of an editor where the user can drag and drop objects over a canvas, and change N properties on the objects. How can I know if the user has made changes on the app in order to prompt him whether he wants to save these changes or not? One way is to create a IsDirty property but this means I would have to add a line of code in many places, maybe another way could be create a virtual save, and then compare the real saved file with the virtual 开发者_StackOverflow社区saved file; is there another way?


A single IsDirty flag that gets set to true when anything changes would be the right way to do this. It is the least expensive way to track this piece of state.

Saving would set it back to false.


I would prefer the second option that comparing the virtual file with the real saved one, because it's easier than set IsDirty flag in everywhere (all possible event handlers) and you might forget set it later after some changes.


I think both technics you described are ok, the IsDirty method could be very fast at runtime because you do not have to serialize and compare, it could imply some changes around but it's not bad by design.


If the user changes some property and then changes it back to its original value, the IsDirty flag would indicate that he needs to save, while the "virtual save" method would indicate that he does not need to save.

Which one you choose partially depends on what semantics you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号