ManagedViewModel.GetUniqueViewModelIdentifier

Dec 14, 2011 at 7:44 PM

Hi Geert,

I have another weird one...

I have an ItemsControl that contains a collection of UserControl<TViewModel> objects that all get created as my app loads.  The UserControl objects are all of the same type.  Occasionally, when I load the app, I get an error in ManagedViewModel.AddInterestedViewModel, at this line:

_interestedViewModels.Add(vmId, viewModel);

The problem is that two of my ViewModels are sometimes getting the same vmId because their ViewModelCreationTime.Tick property is the exact same value, and they are of the same type.  I guess these viewmodels get created awfully fast (they are pretty simple). 

I think the ManagedViewModel.GetUniqueViewModelIdentifier method needs to use a random number in addition to, or in place of, the ViewModelCreationTime to generate the hash.

Coordinator
Dec 15, 2011 at 11:26 AM

I have developed hundreds of view models, but never walked into this issue. Can you please send me the view model (so I can emulate this in a unit test) and fix the issue?

See this issue:

http://catel.codeplex.com/workitem/7050

Thanks in advance!

Dec 15, 2011 at 2:23 PM

Yeah, I thought it was strange, as well.  What is especially strange is that it happened rarely at first, then suddenly started happening almost every time I load the app.  The app is compiled in Debug mode, and run from Visual Studio 2010. 

I am happy to send you the ViewModel, but I have a feeling this is going to be a difficult issue to reproduce reliably.   It is probably somewhat based on the hardware.  I am using a laptop with an i7 Q720 processor and 4GB of RAM. 

Is there any downside to just adding a static Random object to the ManagedViewModel class to tack on a random number to the hash generation code? 

How do I get you the ViewModel?

Coordinator
Dec 15, 2011 at 2:27 PM

I will think about creating a unique identifier (for example, the integer of a guid). Then the viewmodel will have its own unique identifier and that one can be used.

You can attach files to the issue I created.

Dec 15, 2011 at 2:31 PM

If I attach the file, will it be accessible by anyone?  There isn't anything that special about the ViewModel, but I would rather have it only accessible by you.

Coordinator
Dec 15, 2011 at 2:40 PM

In that case, I have sent you a message with my email address.

Coordinator
Dec 15, 2011 at 7:40 PM

Really strange to see that such a complex view model can be created in such a short amount of time. But to fix this issue, I have added a UniqueIdentifier property to the IViewModel interface.

The ViewModelBaseWithoutService increases a private static counter so the id always starts at 1 (so you can also get the count of the viewmodels based on the id).