mvvmlight icon indicating copy to clipboard operation
mvvmlight copied to clipboard

Provide a way to remove a command

Open cosminstirbu opened this issue 6 years ago • 5 comments

Hello,

Currently the SetCommand extension returns void.

This means that you can never detach a command.

This can turn into issues when you're using reusable cells (in a UITableView or a RecyclerView) where if you call SetCommand when the cell is configured with the view model, you'll get the same even to be triggered in multiple places.

One suggestion would be to have SetCommand return some kind of EventToCommandInfo that can expose a Detach method.

Thank you, Cosmin

cosminstirbu avatar Jun 19 '18 07:06 cosminstirbu

It's a good idea. I will add this to the backlog

lbugnion avatar Jun 21 '18 08:06 lbugnion

It would be great if there would be a general method to remove a command from an object. I don't know something like:

   // set the command
   myButton.SetCommand("Click", vm.MyCommand);
   ...
   // remove the command
   myButton.RemoveComamnd("Click", vm.MyCommand);

ChristophvanderFecht avatar Jun 26 '18 06:06 ChristophvanderFecht

Being able to remove the commands would be nice, currently I solve this by have a similar SetCommand signature but that returns een IDisposable that than can be kept somewhere till no longer needed and enforces to make sure to clean up which is a big requirement for iOS. Maybe an idea for mvvmlight as well or have it return and CommandBinding that can be disposed and/or have an register/unregister?

It would be great if there would be a general method to remove a command from an object. I don't know something like:

   // set the command
   myButton.SetCommand("Click", vm.MyCommand);
   ...
   // remove the command
   myButton.RemoveCommand("Click", vm.MyCommand);

Wouldn't that result in references being kept somewhere static in memory? From the Xamarin.iOS would prefer that such to be avoided as can easily get into situations where things are not removed as it is not true JIT.

Duranom avatar Nov 13 '18 13:11 Duranom

I would love to see this implemented as well. Setting commands in table view cells is a very common scenario, and every time a cell is reused currently, the "SetComman" simply adds new target, as opposed to replacing it.

tmarkovski avatar Feb 08 '19 17:02 tmarkovski

Does it mean SetCommand extension causes memory leaks even when not used in reusable cells? Currently we see memory leaks using SetCommand in CachingViewHolder. However, if under the hood it is simple control event subscription each command should be always removed (the same us control event handlers). Even when it is used on simple view (like Login button click command).

Can someone clarify what is best practice here?

oleksandrmykhaylyshyn avatar May 20 '19 12:05 oleksandrmykhaylyshyn